University of Washington
 Search | Directories | Reference Tools
UW Home > Computing and Networking > Unix 
 

Shells and Shell Scripts

A Unix shell is a command language interpreter, the primary purpose of which is to translate command lines typed at a terminal into system actions. The shell itself is a program through which other programs are invoked. Although there are several different Unix shells, among them the C shell (csh), the Bourne shell and the Korn shell, the one most frequently used on Blake within Berkeley UNIX is the C shell.

The shell has some built-in functions, which it performs directly, but most commands that you enter cause the shell to execute programs that are external to the shell. This sets the shell apart from other command interpreters, because it is just another user program at the same time that it functions almost exclusively as a mechanism for invoking other programs.

For more information about the C shell, refer to the article ``An Introduction to the C Shell,'' by William Joy, in the Unix User's Manual Supplementary Documents volume. Also see `man csh' online.

What is a Script?

One handy thing you can do with the shell is to use standard UNIX commands as building blocks to create your own new commands. To do this, you write a `shell script', which can contain a number of commands, and then the file can be executed as one command.

Following is an example of a very simple shell script, which gives a brief indication of the types of things you might do. To use this feature to its maximum potential, however, you will need to refer to the documentation mentioned above, or other Unix manuals.

Suppose your name is joanx and occasionally you want the system to tell you your user id, what directory you are in, list the files in that directory, and display the date. You want each of these pieces of information to be preceded by an appropriate statement. The individual Unix commands you need are `echo,' `who am i,' `pwd,' `ls,' and `date.' So you would create a a regular text file (let's call it `me') with the following lines in it:

     echo you are
     who am i
     echo The directory you are in is
     pwd
     echo The files in this directory are
     ls
     echo The date is
     date

You (joanx) can execute the file called me, when you are in the same directory it is in, simply by entering

     sh me

When you do this, the following information might be displayed on your screen:

     you are
     joanx   ttyB7   Jun  9 14:51
     The directory you are in is
     /b2/joanx/mine
     The files in this directory are
     herfile  hisfile  myfile  ourfile
     The date is
     Fri Jun  9 16:10:50 PDT 1989

Once you learn to create and use them, you should see that shell scripts are useful Unix tools that might save you a lot of time in your day-to-day computing.

More on Scripts

Virtually any command which can be executed at the unix prompt can be executed within a shell script.

Structure

Each line of the script file is a single command. There are two exceptions to this statement. The first is when a line ends with a backslash "\\", since the backslash removes the significance of the newline character as a command terminator. Therefore, a backslash at the end of a line is the indication of command continuation on the following line, just as it is at the prompt.

The second exception is when the "<<" I/O redirection symbol is used in a script file. This symbol will be discussed below.

Comments

Comments in C shell scripts begin at a pound sign "#" and continue to the end of the line. Since most Unix system support several shells, it is recommended that each shell script start with a comment indicating which shell the script was written for.

Output

Text can be sent to standard output from within a shell script by using the c-shell command 'echo'. This command will print the value of its arguments on standard output appending a newline. The -n option to echo will suppress the newline at the end of the text.

Examples

The following script will print out the name and contents of the current working directory.

     # print the name and contents of the current
     # working directory
     echo "This is from a shell script:"
     pwd     # print the directory name
     ls      # print the directory contents

The following script will edit a C source code file named "count-c", compile and link it producing an executable version named "count", and finally run count.

     # edit compile link, and run
     vi count.c
     cc count.c -o count
     count

Running Scripts

There are three ways to execute a c-shell script.

Direct Execution

The simplest way to execute a shell script is to used chmod to make the text file executable. The script can then be executed by typing its name if it is in the current working directory or is in the path for that shell.

Executing a script creates a new shell and executes each command in the script within the new shell. When the end of the script file is encountered, the new shell exits. Any changes in the new shell caused by the script are lost when the shell exits.

For example, if the file
/usr/samples/script/change contains...

     cd/usr/games
     pwd
     fortune

Then the command sequence would yield the following output:

     mead> cd   /usr/samples/script
     mead> pwd
     /usr/samples/script
     mead>  chmod  +x change
     mead>  change
     /usr/games
     To err is human. To forgive, divine.
     mead>  pwd
     /usr/sample/script

csh Execution

Another way to execute a script within the context of a new shell is to run csh and give it the name of the script file as its first argument. This is, in fact, how direct execution is implemented. By executing csh directly, the default csh options can be modified. Then the command sequence would yield the following output:(user input is in bold) (the -x option causes echo to be set, i.e each command will be echoed before it is executed)

     mead>  cd  /usr/samples/script
     mead>  pwd
     /usr/samples/script
     mead>  csh  -x change
     cd /usr/games
     pwd
     /usr/games
     fortune
     To err is human. To forgive, divine.
     mead>  pwd
     /usr/samples/script

source Execution

The third way to execute a script will execute it within the context of the current c-shell, using the c-shell command source and giving it the name of the script file as its first argument. Since execution takes place within the context of the current shell, any changes in the shell are retained following the completion of the shell. Then the command sequence would yield the following output:

     mead>  cd  /usr/samples/script
     mead>  pwd
     /usr/samples/script
     mead>  source change
     /usr/games
     To err is human.  To forgive, divine
     mead>  pwd
     /usr/games

Non-script Execution

There are several ways to execute commands within shells or at the command prompt.

Sequential Commands

A series of independent commands can be grouped together as a single command by separating them by semicolons ";".

Pipes

Pipes "|" also cause the execution of multiple processes from a single command line. With a pipe, the standard output of the process before the pipe is redirected to the standard input of the process after the pipe. This is also covered below, in I/O Redirection.

     mead>  /fortune | tee temp  ; wc temp ; rm temp
     Save the whales.  Collect the whole set.
            1       7      41

Inline Execution

Grave accents "`" (on the same key as the tilde "~") will force inline execution of the enclosed command. Like the quotation marks "''" and the apostrophes "'", grave accents must be used in pairs. Grave accents are used where a command "is not expected".

     mead>  set code = ( `ls *.c`)

Subshells

Any commands enclosed within parentheses are run in a separate shell, similar to the "direct" or "csh" execution of a shell script. As you can see, technically, this is not a means of running a shell script at all, but it is similar. The following sequence of commands illustrates this. Recall the use of the semicolon to separate sequential commands.

     mead>  cd   /usr/samples
     mead>  pwd
     /usr/samples
     mead>   (cd  ; pwd)
     /users/unix0
     mead>  pwd
     /usr/samples
     mead>   cd  ; pwd
     /users/unix0
     mead>  pwd
     /users/unix0

Deferred Execution

There are times when variable substitution must occur before command execution. The c-shell command eval causes execution to be deferred until after variable substitution. Variable substitution is discussed in greater detail in the next section.

Redirection

Input and output redirection is available within shell scripts.

Input and Output Redirection

Input redirection will read from the named file instead of from the terminal. Input redirection uses the symbol "<". Output redirection uses the symbol ">" to create the named file to contain the output of the process. Output redirection can clobber (overwrite) an existing file. Output will be appended to the named file when the output redirection symbol ">>" is used.

Pipes and Filters

Piping connects the output of one process to the input of a second. Piping uses the symbol "|". A filter is a process which is between two pipes. It simply changes the information coming down the pipe.

Standard input for a process can be drawn from the script file itself. This uses the special redirection symbol "<<". The name which follows the redirection symbol is a tag for the end of the input. That is, the contents of the script file will be read as standard input to the process until a input line matching the name following the redirection symbol.

If the file /usr/samples/script/redirect contains:

     cd
     echo "Here are some commands:"
     sort << end > temp
     pwd
     ls
     clear
     passwd
     end
     cat temp

The following is an example of using the 'redirect' script:

     mead>  cd /usr/samples/script
     mead>  redirect
     Here are some commands:
     clear
     ls
     passwd
     pwd
     mead>  pwd
     /usr/samples/script

[University of Washington]
Computing & Communications
help@cac.washington.edu
Modified: January 23, 2002