The title of this post is a little misleading, what we are going to do today is develop our own Bash command to work in Terminal to allow use to do simple, repetitive commands, with only a couple of keys. Think of they as macros, a simple and easy way to cut down time. This doesn’t take long and it can be powerful, and like anything in life it just takes a little bit of know how. This tutorial is great if you have a specific folder which you visit again, or a Terminal command you run frequently. For example I have one where I type “hits” to get the current number of connections to my site, the actual code to do this is a bit long.
First things first open Terminal in Applications > Utilities. Then type ls -a you should see a lot of files. Depending on the version you use you should see a file called either “.bash_profile” or “.profile”, remember which is one is present. If none are present it doesn’t matter. Then type
nano .bash_profile into Terminal to open up a text editor. If “.profile” is there try
nano .profile. It is probably worth saying now, if this doesn’t work with one file name, try the other. I’ve read various different tutorials, all referencing a different file. My bet is on “.bash_profile” if you are on Mac OS X 10.5/6.
This file controls some of the aspects on how Bash works. Bash is the program that runs within Terminal, there are other ones but we wont get into that. What we are going to write is a macro/function/what-ever-you-want-to-call-it which will run hen the command is typed. Your bash profile may have some text in it already so scroll down to the bottom with the arrow keys if it has. Then write the following:
This is the beginning of our code. It will run when we write test into Terminal. Then write the following on a new line (change it as you wish).
Change [name] to your name, or change the whole path to something you know exists. Make sure the closing curly brackets is on a new line. To finish press Control + O. Your changes will now be committed. To test them open a new Terminal window by pressing Command + N, and type test into the Terminal prompt. If everything goes to plan, the new directory will be picked. By opening a new window you can test any changes while still having the original text editor open. Any errors will be at the top of the Terminal prompt when you open a new window.
Now you have the workings we can make things more interesting. Anything on the “cd /Users…” line change be changed to anything you want. You can have more than one command by ending them in a semi-colon and putting the second command on a new line. Always make sure that the curly bracket is on a new line and the name you give is unique.
If you want to make things a little bit more complicated you can. In the next example I am going to use a variable. Variables are represented by “$1”. For example the following:
Will work when you type into Terminal:
The first word after “test2” will take the place of the $1 symbol. You can add more than one variable as such.
test2 James Desktop
Will result in Terminal in terminal changing the directory to my Desktop. The variables can appear any number of times, just put them in speak marks.
Have a play around with the commands, over time they will be come more complex. If you want to remove anyone simply delete the entry. To close the text editor press Control + X. Note changes you make will only take effect when a new Terminal prompt it open.
If you want to learn more there is hundreds of books around. Simply google “Bash Programming”, or check out some books in Amazon, some good ones include Pro Bash Programming, Learning the bash Shell, Bash Cookbook or any bash programming on Amazon.
If you have any questions or comments please leave one below.