Chapter 3. The Bourne Shell

Table of Contents

Command Line vs Shell Scripts
Shell and Environment Variables
Quoting
Wildcards
I/O Redirection
Multiple Commands, Sub-commands and Background Commands
Control Flow
Arithmetic
Shell Functions
Examples

This section describes the basics of using the command processor called the Bourne Shell (hereafter simply called "the shell"). Note that although the Bourne Shell is the standard command processor on many Unix systems, it is possible to use other shells under Unix. Three of the more popular alternatives are the C Shell (csh), the Korn Shell (ksh), and the Bourne Again Shell (bash). Of these, the Korn Shell and the Bourne Again Shell are upwardly compatible with the Bourne Shell. It is also possible to use the Bourne Shell under other operating systems. For example, Windows implementations of the Bourne Shell exist. This document assumes you are using the shell under Unix.

Command Line vs Shell Scripts

The shell allows you to execute any command interactively. Since some commands require more than one line, the shell uses a secondary prompt to accept the additional lines of a multi-line command. For example

$ for FILE in *
> do
>   cp $FILE $HOME/backups
>   echo "Saved $FILE on `date`" >>$HOME/.backupinfo
> done
$

This command was typed interactively. However, since it involves a loop, the shell could not act on the command until it saw the "done" keyword. Thus the shell responded with the secondary prompt (a >) while it collected the entire command.

There is no limit to the size of the command or the number of lines or the number of nested control structures that the shell can accept interactively. However, you can only edit the current line. Once you've typed ENTER, that line is committed. You won't want to enter huge commands interactively unless you are a shell virtuoso.

For more complex operations, you will want to create a shell script. This is a text file that contains the commands you want executed. The first line of the file should be

#!/bin/sh

This line informs the operating system which program will handle the script. For example, if the first line looked like #!/bin/csh, the C Shell would be used to handle the script. As a result you can run scripts for other shells than the one you are currently using.

In a shell script, all material on a line after a # character is ignored (except for the first line, as described above). Use this feature to include comments in your script.

Before the shell can execute a script, you must have execute permission on the file containing the script. You can grant yourself execute permission using the chmod program. The easiest way is to use chmod's symbolic mode:

$ chmod u+x scriptfile

The "u+x" grants (+) execute permission (x) to the user (u) who owns the file. No other permissions are affected. You can also grant execute permission for people in the same group as the file with

$ chmod g+x scriptfile

or even

$ chmod ug+x scriptfile

to do both the user and the group at the same time.

Once you have execute permission to a file, you don't normally need to set it again after editing or copying the file.