oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

An Introduction to Tiger Terminal, Part 4

by Mary Norbury-Glaser

In parts one, two, and three of this series, we covered basic commands that can be executed through the OS X Tiger Terminal app. Today, I'll take a bit of a leap and introduce shell scripting.

What are shell scripts, and why would you want to create them? Well, if you begin to use the Terminal with regularity, you'll find that you are repeating many of the same commands, often in sequence, in order to accomplish some task. You can automate these tasks by writing a shell script file.

What You Need To Know--And Do

In part one of this series, we ran the cat /etc/shells command to see a list of shells available in Mac OS X Tiger. Each shell has a standard scripting language associated with it. Since our default shell in Mac OS X Tiger is bash, we'll stick with that for this scripting tutorial.

You'll also need a text editor to write your scripts. In part one, I introduced Mac OS X Tiger's new default text editor, nano, which we'll use here to write and save our new shell scripts. You can use any text editor, whether it's a command-line editor or a graphical editor like SubEthaEdit ( or BBEdit ( If you choose a graphical editor, make sure you select the option to use Unix line breaks when saving files.

Let's assume you'll be creating shell scripts on a regular basis, so it's the best practice to create a directory where you'll save all your scripts. Create a new folder in your /Documents directory and name it scripts. From the Terminal command line, now!

norburym15:~ norburym$ cd Documents
norburym15:~/Documents norburym$ mkdir scripts
norburym15:~/Documents norburym$

Remember that your default location when you open the Terminal app is your home directory. Here, you've changed into your Documents directory and then created a new directory (folder) called scripts, using the mkdir (make directory) command. The Terminal doesn't tell you it's been created nor does it move you into the directory automatically. Run the ls command at the last prompt above to see that the new directory has, indeed, been created:

Figure 1. ~/Documents/scripts folder

The next thing you'll want to do is tell the shell where to find this directory. You'll do that by adding the location of your new scripts directory to the PATH variable in your .bash_profile file. Huh? OK, good time for an aside! When you open the Terminal app, the default shell, bash, starts up and reads some files in order to get configuration information. The primary files are bashrc and profile, which live in the system /etc directory (partial listing shown below):

Figure 2. bash files

However, to set user-specific configuration variables, it's best to create a file in your own home directory that bash will read when it starts up. bash will read the system-wide files first, and then the user-specific ones. The user-specific file settings will override those in the system-wide files.

You can see your current PATH by executing the echo $PATH command at the shell prompt:

norburym15:~ norburym$ echo $PATH
norburym15:~ norburym$

Pages: 1, 2, 3, 4, 5

Next Pagearrow