Articles‎ > ‎

Shell Scripting 101: Part 1

posted Dec 19, 2008, 5:38 AM by Philip Rinehart   [ updated Dec 19, 2008, 5:38 AM by Greg Neagle ]
Written by Philip Rinehart   
Wednesday, 11 April 2007
One of the most common requests from many individuals that are learning to use and administer Mac OS X is how to write a shell script.  In Part 1 of the series, we will look at what a shell script is, why one would use a shell script, ending with a tutorial in basic shell script writing.

What is a shell script?

What exactly is a shell script in the most basic sense?

It is a series of commands written in such a way that they can be executed in a repeatable process.  The focus of this series is the use of the shell only, specifically the use of the default shell in Tiger, bash.

Why use a shell script?

This is the second most asked question about the art of shell scripting.  There are two primary reasons that stand out for the use of shell scripting:

  1. Simplicity.  Most shell scripts are collections of commands that an administrator or user enters at the shell prompt in the Terminal application.
  2. Rapid development.  As most shell scripts are collections of commands, it is usually very easy to rapidly prototype any action, for the same reasons that most shell scripts can be quite simple.

Creating a simple shell script 

To start, open the Terminal application to start a new session with a shell.

Next, let's use a quick trick to create a file, using the cat command.  It makes prototyping scripts very simple, and can serve as an on-the-fly editor without learning vi or emacs.  Type the following: 

			cat > newscript

This creates a file called newscript.  Any command typed after entering the above command will be recorded into the new file.  Believe it or not, you've now just created your first shell script!  But, wait, there aren't any commands!  Fix that by typing in a command to list all files and directories and then count them:

			ls -l | wc -l

That's it, it's all that this script is going to do.  But wait, if more commands are entered they will be recorded to the newscript file!  If using this method of script creation, terminate the file by entering the end of line character  Ctrl-D.

Almost done!  To test that the file will actually work, type:

			sh newscript

 This should then return the total number of files and directories of the directory you created the script in.  Can you run this in other ways?  Sure, but for your first shell script, give this method a try.


To make a shell script self contained, one extra line needs to be added.  It should appear at the top of the file, and is referred to in Unix parlance as the "shebang", or #! After the #! set of characters, the full path of the interpreter is used to execute the set of commands which appear after the first line.  Since we are talking about shell scripting, here is an example:

			#! /bin/bash

This command at the top of any shell script file will use the bash shell to interpret any commands after they are entered. 


Before ending Part 1, an important part of shell scripting is using redirection and pipelines.  Let's deal with the simplest first:

<  Entering this character causes the command to redirect standard input (STDIN), accepting a file name as an argument.  Example:

     wc -l < filelines.txt 

 This example count the number of lines in the filelines.txt file, using the unix command wc.

> Entering this character causes the command to redirect standard output (STDOUT), accepting a file name as an argument.  Example:

			ls -l | wc -l > numberoffiles.txt

This example will take the output of ls -l | wc -l and put in into a text file, numberoffiles.txt 

>> Entering these two characters causes the command to redirect standard output  as above.  If the file does not exist, it is created; but if it the file does exist it will append to it.  Example:

     ls -l | wc -l >> numberoffiles.txt

This example appends the output to the end of the file numberoffiles.txt 

| The pipe command.  Entering this character takes the standard out of one program and uses it for the standard input of another.  Example:

    ls -l | wc -l

This example takes the output of ls -l, which is then used by the unix command wc, counting the number of lines output by ls -l.

One last thing

The last thing to examine is a special variable, known as an environment variable.  Often, shell scripts inherit a variable known as the PATH.  What is it?  It is the directories that are searched when looking for unix commands.  It is generally considered good practice to enter a PATH statement as the second line of any shell script.  A typical example would be:


This will reset the path for the shell script, so that alternate directories are not searched, a potential security risk.  As a final note, here is a full example combining all of the techniques in this article.

			#! /bin/bash
ls -l | wc -l > numberoffiles.txt

In this example, line 1 uses the shebang, line 2 sets the PATH variable, and line 3 uses a command and redirects it output to a file.  Part 2 will focus on variables, special files, and ways of combining input redirection.  Until next time... 

Last Updated ( Thursday, 03 January 2008 )