Create new commands for your Unix-based command line

Do you ever wish you had a shortcut to save yourself from typing the same things into the command line repeatedly? This tutorial will show you how to create a new command for your Unix-based command line (Linux, OS X, etc.).

Here is our scenario:

We are developing code for a client and managing it with some branches on GitHub. The code lives on our local machine in /Users/myUserName/clients/myClient/code. We have Git branches for development and production, and another, local branch where we write experimental code. We are on version 4.5 of this project. So we find ourselves typing the following commands into the command line A LOT:

$ git checkout myClient_4.5_development
$ git checkout myClient_4.5_production
$ git checkout myClient_4.5_local

…but we would much rather simply type…

$ dev
$ prod
$ loc

…to achieve the same thing.

To do this, we need to create some scripts that will contain the git commands, make sure they are executable, and then add the directory where they live to the $PATH variable.

1. Navigate to the directory where we keep everything related to our client.

$ cd ~/clients/myClient

That “~”, just in case you don’t know, is a shortcut to our home directory located at /Users/myUserName

2. Add a directory for the shell scripts and navigate into it.

$ mkdir scripts
$ cd scripts

3. Create the files for our scripts.

$ touch dev
$ touch prod
$ touch loc

4. Make the scripts executable…

$ chmod 700 dev

…and repeat, as in step 3, for prod and loc.

5. Edit the shell script with whatever text editor you are comfortable with. People like vim, pico, nano, emacs… or you can go the long way around and use TextEdit or some other, more GUI-oriented editor. So I might normally use vim, but it can be a little bit difficult for those unfamiliar with it, so let’s use nano instead.

$ nano dev

Whatever text editor you choose, we want the following in our scripts…

git checkout myClient_4.5_development

…and we would change “development” to production or local for the prod and loc scripts.
That first line is the shebang, which tells the command line what program to use to interpret our file. In this case, we want the shell script interpreter (sh), and we typically find that within the root bin file (/bin).

6. Now we simply need to add our scripts directory to the $PATH variable, so the command line will know where to search for dev, prod and loc. To do this, we need to edit our profile file. This might be called .profile on your system, or it might be called .bash_profile, or something similar. You will find it in your home directory.

$ nano ~/.bash_profile

We need to add the following line to our profile.

export PATH=/Users/myUserName/Clients/myClient/scripts:$PATH

We’re done! Now we can open a new terminal window, navigate to our code directory and type our new commands to switch between Git branches. When our version number changes, we simply edit our scripts accordingly.

I’m not the Unix master I would like to be, so if anyone sees a way to improve this, please let me know in the comments and I will update this post. Thanks!

This entry was posted in Unix, Web Development and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>