Skip Navigation
 Search | Directories | Reference Tools
UW Home > Discover UW > IT Connect 

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
     echo The files in this directory are
     echo The date is

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
     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.


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 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.


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.


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

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...


Then the command sequence would yield the following output:

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

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)

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

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:

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

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 "|" 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.

     unix>  /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".

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


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.

     unix>  cd   /usr/samples
     unix>  pwd
     unix>   (cd  ; pwd)
     unix>  pwd
     unix>   cd  ; pwd
     unix>  pwd

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.


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:

     echo "Here are some commands:"
     sort << end > temp
     cat temp

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

     unix>  cd /usr/samples/script
     unix>  redirect
     Here are some commands:
     unix>  pwd