Saturday, November 05, 2005
A Simple Introduction to The OS X Terminal and Bash Scripting: Part :
Categories: Life • Technology
Note:
This will be oriented to Mac users running OS X. I will be specifically focusing on the most recent version (Tiger/10.4). However, most of this will work on any Unix based system with the Bash shell available for use.
I first started using a Unix based OS my freshman year of college. Our school email was at the time completely Unix based. We used a program called PINE (written by the University of Washington and still in use in many schools and businesses today) that ran on top of Unix terminals that were spread throughout the campus grounds. We could also telnet to our email from our Macs and PCs from our dorm rooms. Needless to say it wasn't long before we discovered that email wasn't all we could do.
By typing who and pressing Enter we were given a list of everyone else currently logged onto a terminal or connected by telnet to the email server. For kicks open up your Terminal in OS X and try it out. You shouldn't see anyone but yourself.
It wasn't much longer before we discovered that there is a small chat utility built in called talk. All we had to do was type talk put a space and then type someone's username (while they were on the system) and they would get a request to chat which they could accept or deny. Pretty soon it became harder to pay attention in technology related classes. This is when AIM and ICQ were basically brand new. We didn't even know they existed yet. So this chatting ability was pretty neat stuff.
Before long we discovered aliases and a whole list of Unix commands/apps that allowed us to do things the administration probably hadn't counted on. There will be more on aliases in a minute.
At the time I was pretty busy with other things so I was content with this limited knowledge. However, recently I've needed to do more advanced things with Unix both at work and through my home systems and my OS X based web-server on which I host this and my other websites. I went in search of good tutorials that gave a concise idea of how to write Shell scripts. My search was in vain. After slogging through some tutorials written by masochistic Unix gurus I managed to cobble together some basic Bash, Unix, and Shell scripting knowledge. I'll be attempting to impart that knowledge to you.
Conventions
These are the conventions I will try to follow throughout this entry:
When I type a Unix command I will use
text like this. When I type a keyboard command that you need to press I will use text like this. (When I tell you to press Enter you may use either the Return or Enter keys.) When I type output that you should get from an application or command I will usetext like this(the same as for a Unix command).
Your Shell, Aliases, and You
Lets start by opening up your Terminal. Go to the Applications folder then go to the Utilities folder and double click on the Terminal application.
Once it is Open type echo $SHELL and press Enter.
If you are in Mac OS 10.4.x you should get an answer like this:
/bin/bash
The echo $SHELL command indicates which Shell you are currently using.
If you received a different answer then you are not in the Bash Shell. If Bash is available on your system you should be able to get into it by typing bash and pressing Enter. The prompt on the terminal will likely change after doing this.
The Shell is basically a text based front end to a Unix OS. Bash is one of the more recent Shells. It isn't the most powerful but it is relatively easy to use and has some nice time saving features.
Try typing echo hello world! and pressing Enter
The Terminal window should repeat "hello world!" and then return you to the prompt.
Now type banner hello world! and press Enter.
Enlarge your Terminal window so you can actually see what the output looks like.
By this time you have already created a history of commands that you have used. Try pressing the Up Arrow once. You should now see this in the prompt:
banner hello world!
Pressing Enter would run that command again. If you press the Up Arrow again it will show the command before that. Again and the command before that will be shown. I'm unsure how far back it will remember but it will remember quite a ways back. It also remembers across sessions. So if you quit the Terminal application and then start it back up pressing the Up Arrow will still show the previous commands as if you had never quit.
Bash also provides some things to help you navigate directories using the prompt. Obviously you can't see the contents of the directory you are in since this is a text based interface. Type ls and press Enter.
That will give you a list of the folders and files in the directory you are currently in. By default this directory is the home directory.
If you type cd ../ and press Enter you will be moved down a directory to the Users directory.
Go ahead and go back another directory using the same command. You will now be in the base directory of your system.
Type ls and press Enter again and see what is in the directory.
You should see a list similar to this:
Applications Desktop DF Network System Volumes
bin dev mach mach_kernel sbin
usr Desktop DB Library Shortcuts Users
automount cores etc mach.sym private
tmp var
You are in the same directory you would be in if you double clicked on your main hard drive in the finder. As you can see there are quite a few more directories and files than you see in the Finder.
After awhile you get to know where directories and files are without needing to do an ls command every time you enter a new directory. Bash takes this into account and provides a handy way to make it easier to type out whole names.
For instance if you type cd Use (Yes, just type Use) and press Tab on your keyboard Bash will type out the rest of the Directory for you. You can even continue from there. You already have cd /Users/ now type the first few letters of your short name (the name of your Home folder) and press Tab again. Pretty cool huh? Or maybe I'm just easily amused.
Go ahead and press Enter and you should now be back in your home folder. Unix has a built in short cut for getting back to your home folder from anywhere. You do this by typing cd ~/ and then pressing Enter. At first I found that hard to remember and typing a ~ requires holding down the Shift key which is kind of a pain if you are lazy. So I have an Alias that allows me to type home instead of cd ~/.
Aliases are kind of like mini scripts. They let you string together a command or commands on one line and then assign a short keyword to the command(s). For example as I stated above I assigned the command cd ~/ to the keyword home.
In OS X the Bash shell will look for a file call .profile in your home folder. If it find that file it will look through the file for any special settings you want used. One of the types of special settings it looks for is Aliases. There isn't a .profile file by default so you will need to create it.
While in your home folder in the Terminal type pico -w .profile and press Enter. This will open a new document called .profile in the pico text editor. Pico is a fairly basic relatively easy to use Unix text editor. Putting -w after the name of the editor turns off the word wrapping which is important when doing any kind of coding.
Once you have launched pico you should see something like this (Actually in the case of Mac OS 10.4.x an editor called nano is launched instead. Nano is more advanced editor based on pico.):
GNU nano 1.2.4 File: .profile
[ New File ]
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Txt ^T To Spell
The ^ character corresponds to the Ctrl key on your keyboard.
Now lets create an alias. Type the following into the Terminal:
alias home="cd ~/"
This creates an alias called home that runs the command cd ~/ which will send you back to your home folder from any directory.
You have to follow the format exactly or your aliases wont work. Each alias needs to be on its own line as well. Press Enter to go to a new line and add this alias:
alias picow="pico -w"
This creates an alias called picow that runs the command pico -w which will edit a file that you specify after the alias with the word wrapping turned off.
Save the file by typing Ctrl X type Y and press Enter. Now close the Terminal window and open a new one. Every time a new Terminal window opens it reloads the .profile file. Now if you type picow .profile it should open the .profile file again with the word wrapping turned off.
The two aliases above are more for convenience sake than anything else. It is slightly eiser to type home than it is to type cd ~/, but it isn't a lot easier. The same thing goes for picow and pico -w
However, some cases using an alias can save you a lot of time. Take an application such as gzip for instance. gzip is a decompression/compression utility that has a lot of different options.
Lets say I often need to run gzip with five options turned on -r -q -N -S and -9. These options turn on recursive folder treatment, quiet running (it doesn't give you any errors even if they exist), and best compression and a few other more obscure things.
I could do this by typing gzip -rqNS9 every time. Or I could create an alias like this:
alias compress="gzip -rqNS9"
Now I can run that same command by simply typing compress and I don't have to remember the string of options every time I compress a file or folder using gzip.
You can even combine multiple commands into one alias using a semicolon (;
). For instance lets say I have two servers that I want to ping 3 times every once and awhile to make sure they are working. I could do that by typing ping -c 3 169.254.0.1 pressing Enter and then typing ping -c 3 169.254.0.2 and pressing Enter again.
These two commands could be combine like this:
ping -c 3 169.254.0.1 ; ping -c 3 169.254.0.2
So I could create a new Alias in my .profile file like this:
alias pingmyservers="ping -c 3 169.254.0.1 ; ping -c 3 169.254.0.2"
So my .profile file would now look like this:
GNU nano 1.2.4 File: .profile Modified
alias home="cd ~/"
alias picow="pico -w"
alias pingmyservers="ping -c 3 169.254.0.1 ; ping -c 3 169.254.0.2"
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Txt ^T To Spell
So there is your introduction to Bash, and Aliases. In the second part I'll go over actually writing some simple bash scripts. And in the third I'll go over some more advanced stuff. I'm interested in hearing if this was at all helpful to you. So leave a comment if you like. Or tell me what you would like to learn and I'll possibly know enough to teach how to do it. :)
I also encourage you to check out all the commands and applications that are available to you. There is some pretty neat stuff you can find with a simple Google search for Unix commands.
Posted by Jamie at 05:31 PM
comments
An informative post, I’ll be sure to read it in more detail during all my free time at work.
Posted by Thranx on November 16, 2005 at 02:03 AM
Hi
It was very helpful for me. Good work. Thanks.
Posted by Marshia on December 27, 2005 at 03:54 AM
Glad it was useful Marshia. :)
Posted by Jamie on January 18, 2006 at 10:31 PM
Very helpful for me too. I’m trying to set up Subversion on my machine here and I’d no idea even what the .profile file was. So thanks!
Cheers,
Rob
Posted by Rob on April 06, 2006 at 06:34 AM
Thanks allot! This is an amazing resource...nice and simple yet very useful!
Posted by adamg on June 09, 2006 at 06:17 PM
Where is part 2 and 3? I thought that your intro provided a great starting point, now lets dig deeper!
thanks!
Posted by TJ Lava on August 10, 2006 at 01:38 PM
Alright you have motivated me. :)
My next blog entry will be part 2. I’ve been meaning to finish this for some time but I’ve just been so busy and entries like this take a long time.
Posted by Jamie on August 10, 2006 at 01:43 PM