loading...

Creating a Bash Command

jacobknaack profile image Jacob Knaack ・1 min read

Let's walk through the process of adding a fresh baby bash script as a command that you can run in your terminal.

Say you have a super rad script that does mega cool things somewhere on your computer:

hello-world

#!/bin/bash


echo Hello world

Allowing your bash terminal to run this script file as a command just takes a few simple steps.

1. Place your script somewhere that scripts go.

I recommend creating a directory at ~/ called bin or .bin. The bin folder is typically where commands go, so I'm going to replicate this:

$ cd ~/

$ mkdir .bin/

$ mv path/to/script ~/.bin/YOUR-SCRIPT-NAME

2. Add this Directory to your PATH.

Add a line to your .bash_profile that will make sure your bash terminal has a path to this file when running your command.

If you don't have one create one with

$ touch ~/.bash_profile

.bash_profile

#Here's the line you should add:
export PATH=$PATH:/Users/YOUR-USERNAME/.bin

Now you can either restart your terminal at this point or source this file:

source ~/.bash_profile

3. Add Permissions for Users to run this command.

We'll use the illusive chmod command to let users run this newly created command:

$ chmod u+x ~/.bin/YOUR-SCRIPT-NAME

And that's it! You should now be able to run your script from anywhere in your terminal.

Posted on by:

jacobknaack profile

Jacob Knaack

@jacobknaack

I am a Web Developer in Seattle. I spend much of time at Code Fellows downtown campus where I teach and build fun and sometimes useful things.

Discussion

markdown guide