[Next Chapter] [Top page] [Previous Chapter]



The role of the shell

A Unix shell is used to:

evaluate the command line. For example:

%  car  nofile
car: Command not found

Here the shell looks for a command called car. Since it cannot find this command it gives an error message.

perform variable substitution. For example:

% echo "In directory $HOME"
In directory /home/sunserv1_b/lnp5jb

Here the shell variable $HOME is evaluated and displayed.

handle pipelines. For example:

%  who  |  wc  -l

Here the output from who is piped through to the wc command which displays a count of the number of lines in its input.

Types of shells

A number of shells are available for Unix systems, including:

Bourne shell

C shell

Korn shell

Graphical User Interface (GUI) shells

The Bourne shell, which was developed by Steve Bourne at Bell Laboratories, is one of the oldest shells and, as such, has gained a lot of popularity. It is widely used for shell programming because of its efficiency and because it is available on all Unix systems.

The C shell provides sophisticated interactive capabilities lacking in the Bourne shell. The C shell, which was developed at the University of California, Berkeley, has a syntax which resembles the C language. Features of the C shell include a command history buffer, command aliases and file name completion.

However the C shell does not allow efficient shell programs (also known as scripts) to be written. Due to the fact that C shell programs are written in a style similar to the C programming language, people who are unfamiliar with C may find the C shell difficult to program in.

The Korn shell combines the best features of the Bourne and C shells. Korn scripts are 95% upwardly compatible with Bourne scripts. The Korn shell interactive features include:

in-line editing

command editing

job control

Graphical User Interface (GUI) shells provide a iconic interface to Unix. GUI shells require the use of workstations (or powerful microcomputers) which perform part of the processing locally. The use of GUIs such as X-Windows is likely to become increasingly important in the near future. GUIs currently available include:

Sun View A Sun-specific GUI

Open Look GUI standard supported by Sun

Motif GUI standard supported by other suppliers

Vista eXceed Available on PCs; similar in style to Motif

There is a battle currently taking place in the market-place to establish the standard GUI.

Recommended shells

The Bourne shell is the oldest shell, and is widely used. The C shell has more utilities however and is probably more widely used now.

The default shell for interactive shells at Leeds is the C shell. The Bourne shell is the default for shell programs.

However the Bourne shell is recommended for shell programs. The Korn shell is not widely available and is not a standard part of Unix, but is perhaps the best option if available, unless you want to do a lot of C programming. You can change your default login shell using the command:

% chsh username /bin/sh			Bourne shell% chsh username /bin/csh			C shell% chsh username /bin/ksh			Korn shell

Warning! You probably don't want to try these commands now.

C shell features

The history mechanism

The history mechanism enables previous typed Unix commands to be re-invoked and edited. There are two forms. One is the quick substitution, which acts only on the immediately preceding command, e.g:

%  car  message
car: Command not found
% ^r^t
This is the message file

This command replaces the first occurrence of 'r' with 't' in the last command.

A list of previously entered commands can be displayed using the history command:

% history
1 cd texts
2 vi lookup
3 who
4 history

Commands can be re-entered using the number. For example:

%  !2

will re-execute the second command (vi lookup). It is possible to add extra options to commands re-executed. For example to redirect output from the who command to a file called list we could give the command (for the above list):

%  !3  > list

You may also edit previous commands e.g:

% !2:s/vi/cat/
cat lookup

although it is usually easier to re-type the whole command. The last command may be referred to as !!, and you can count back using !-2, !-3 etc..

File name completion

Within the C shell when a file name is used in a command it is possible to specify only as many characters as will uniquely identify the file, and then press the <ESC> key to complete the filename:

% ls
mbox      message
% cat me<ESC>
This is the message file

When you type <ESC>, the file name will be extended to 'message' on screen.

Command aliases

Command aliases provide a way of customising commands. For example:

% alias dir ls
% dir
mbox      message

Note that command aliases are only valid during the execution of the current shell. It is normal practice to include alias definitions in your .cshrc file.

The following aliases could be useful to shorten long command names:

alias  hh  history
alias  ll  'ls  -al'
alias  q  logout

The quotes around ls -al are necessary because of the space in the command. This tells the shell that it is all one command.


Put the above aliases in your .cshrc file. Think of some other aliases that you would use, such as shortened versions of commands or different names for commands that you will find easier to remember.

C shell startup files

Certain files are executed automatically.

These are:

.cshrc file

Executed whenever a new C shell spawned

Useful for specifying command aliases

Since C shells may be spawned automatically be certain systems commands (such as the mail system of a compiler) this file should NOT contain commands which send output to your terminal.

Contains a list of directories that are searched for commands. A line in the .cshrc file will give a value to the PATH system variable. The user can add pathnames to this list. It is conventional to store any of your own commands or shell scripts that you will use frequently directory called bin, and to add ^/bin to your search path.

.login file

Executed when you login.

Use for setting system wide variables, such as your terminal type.

Can be used to display information, such as who is logged on, or news from the system managers.

Shell processes

A process is an executing program. To display a list of processes use the ps command:

% ps
23268 ttyp1 0:01 ps
22520 ttyp1 0:00 csh

The PID specifies the Process Identifier. The 'time' field gives the amount of CPU used by the process.

Background processes

Normally processes run interactively, but they may also be run interactively, to enable the user to do something else while a process is running (this is known as 'multitasking'). This is usually necessary when you are running a very long job. To run a command in the background use the & character at the end of the command line, as follows:

%  command  &

Note that output from command will still be sent to standard output. If you fail to redirect standard output it will be sent to your terminal where it is likely to be confused with output from your interactive process.

For example, to sort logged on users using a background process give the command:

%  who  |  sort  >  sortedwho  &

Note that this would normally be a very short process and you would not in fact need to run it in the background.

Controlling processes

You may wish to terminate a background process. To do this first you must first find out its process id (PID) using ps:

% ps
23397 ttyp1 0:01 who
23268 ttyp1 0:02 ps
22520 ttyp1 0:00 csh

Then use the kill command to terminate your process.

For example:

%  kill  23397

If the process continues use the -9 argument:

%  kill  -9  23397

Another way of displaying your background processes is to use the jobs command:

%  jobs
[1] + Running who - sort > sortedwho

The background process (or 'job') has been assigned the number 1, and this can be used to refer to it instead of the process i.d.. The job number is usually identified by preceding it with the '%' (per cent) character, so as to differentiate it from a process i.d.. So, for example, the command:

%  kill  %1

should kill this process. A job may also be stopped using ^Z if it is running interactively (you have already met this as a way of stopping vi). A stopped job can be resumed by simply typing it's job number (e.g. %1 to run it in the foreground, or %1 & to run it in the background).

NOTE There are also the C shell commands fg and bg which will bring a job to the foreground and push to the background respectively.

Controlling Processes After Logging Off

If you create a background process and log off the background process will continue to execute. If you log in again and use ps or jobs command to display your background processes, the original background process will not be displayed. The same will happen if you switch between windows when you are using a GUI. This is because these commands, by default, will only display processes that have been created (or 'spawned') by the original login process.

To display background processes spawned by a previous login session you will have to use the command:

% ps -u lnp5jb
lnp5jb 7759 7757  0 10:37:21 ttyw7  0:00 who > sorted who
lnp5jb 5058 5057  0 09:57:02 ttyw5 10:03 longjob
lnp5jb 7760 7758 18 10:37:21 ttyv4  0:00 -csh [csh]
lnp5jb 7798 7760  6 10:37:42 ttyv4  0.00 ps -fu lnp5jb

Special characters

Certain characters have a special meaning to the shell. The backslash (\) is known as the escape character. A character following an escape character has a special meaning. For example:

% echo "This is a very long message \
which is longer than 1 line"

In this example because a command could not fit on one line, the \ character was given IMMEDIATELY before the <RETURN> key was pressed. This indicates that the <RETURN> has a special meaning - which is not it's usual meaning, to terminate the command. The double quotes character (") is used to group words together as a single expression. The single back quote (`) is used to identify a string which is to be executed rather than to be displayed. For example:

% echo "Users logged on are: `who`"


Try this with and without the backquotes around who. Try it with date.

Shell parameters

Parameters can be set interactively in the C shell by using the set command:

% set jenny=/home/sunserv1_b/lnp5jb

To then use a parameter:

%  cd  $jenny
%  pwd

The variable name is preceded by the $ prefix, to indicate that it is a variable. Curly brackets can be used to delimit the variable name if other characters needs to come straight after it. For example:

$ cat ${jenny}/test.dat

Note that the syntax for the Bourne shell is slightly different. You are most likely to use parameters in shell scripts, which you may well be executed by a Bourne shell. The basic difference is that the set command is not used:

$ jenny=/home/sunserv1_b/lnp5jb
$ cd $jenny

Special shell variables

CDPATH informs the shell where to search for the relative pathnames

HOME the name of your home directory

MAIL the pathname of the file where your mail is placed

PATH the list of directories searched for commands

PS1 the primary prompt string

PS2 the secondary prompt string

! the process number of the last process run in the background

# the number of positional parameters

$ the process number of the current shell

? the exit status of the last command run (0 if it was completed successfully, non-zero otherwise).

You can see the values of these variables by typing set (with no arguments). [Next Chapter] [Top page] [Previous Chapter]