An introduction to the C shell
Computer Science Division
Department of Electrical Engineering and Computer Science
University of California, Berkeley
Berkeley, California 94720
is a new command language interpreter for \s-2UNIX\s0
It incorporates good features of other shells and a
While incorporating many features of other shells which make
writing shell programs (shell scripts) easier,
most of the features unique to
are designed more for the interactive \s-2UNIX\s0 user.
users who have read a general introduction to the system
will find a valuable basic explanation of the shell here.
Simple terminal interaction with
is possible after reading just the first section of this document.
The second section describes the shells capabilities which you can
explore after you have begun to become acquainted with the shell.
Later sections introduce features which are useful, but not necessary
for all users of the shell.
Back matter includes an appendix listing special characters of the shell
and a glossary of terms and commands introduced in this manual.
is a command language interpreter.
is the name of one particular command interpreter on
is to translate command lines typed at a terminal into
system actions, such as invocation of other programs.
is a user program just like any you might write.
will be a very useful program for you
in interacting with the \s-2UNIX\s0 system.
In addition to this document, you will want to refer to a copy
of the ``\s-2UNIX\s0 Programmers Manual.''
documentation in the manual provides a full description of all
features of the shell and is a final reference for questions
Many words in this document are shown in
These are important words;
names of commands, and words which have special meaning in discussing
the shell and \s-2UNIX\s0.
Many of the words are defined in a glossary at the end of this document.
If you don't know what is meant by a word, you should look
Numerous people have provided good input about previous versions
and aided in its debugging and in the debugging of its documentation.
I would especially like to thank Michael Ubell
who made the crucial observation that history commands could be
done well over the word structure of input text, and implemented
a prototype history mechanism in an older version of the shell.
Eric Allman has also provided a large number of useful comments on the
shell, helping to unify those concepts which are present and to identify
and eliminate useless and marginally useful features.
Terminal usage of the shell
The basic notion of commands
acts mostly as a medium through which other
commands which it performs directly,
most useful commands are, in fact, external to the shell.
The shell is thus distinguished from the command interpreters of other
systems both by the fact that it is just a user program, and by the fact
that it is used almost exclusively as a mechanism for invoking other programs.
Commands in the \s-2UNIX\s0 system expect a list of strings or
names the command to be executed, in this case the
mail program which sends messages to other users.
The shell uses the name of the command in attempting to run it for you.
It will look in a number of
which is expected to contain the mail program.
The rest of the words of the command are given to the command itself to execute.
In this case we specified also the word
which is interpreted by the
program to be the name of a user to whom mail is to be sent.
In normal terminal usage we might use the
I have a question about the csh documentation.
My document seems to be missing page 5.
Here we typed a message to send to
and ended this message with a control-d which sent an end-of-file to
then transmitted our message.
The characters `% ' were printed before and after the mail command
by the shell to indicate that input was needed.
After typing the `% ' prompt the shell was reading command input from
We typed a complete command `mail bill'.
The shell then executed the
and went dormant waiting for it to complete.
The mail program then read input from our terminal until we signalled
an end-of-file after which the shell noticed that mail had completed
and signaled us that it was ready to read from the terminal again by
printing another `% ' prompt.
This is the essential pattern of all interaction with \s-2UNIX\s0
A complete command is typed at the terminal, the shell executes
the command and when this execution completes prompts for a new command.
If you run the editor for an hour, the shell will patiently wait for
you to finish editing and obediently prompt you again whenever you finish
A useful notion in \s-2UNIX\s0 is that of a
While many arguments to commands specify file names or user names
some arguments rather specify an optional capability of the command
which you wish to invoke.
By convention, such arguments begin with the character `\-'.
will produce a list of the files in the current directory.
to also give, for each file the size of the file in blocks of 512
The manual page for each command in the \s-2UNIX\s0 programmers manual
gives the available options for each command.
command has a large number of useful and interesting options.
Most other commands have either no options or only one or two options.
It is hard to remember options of commands which are not used very
frequently, so most \s-2UNIX\s0 utilities perform only one or two functions
rather than having a large number of hard to remember options.
Many commands may read input or write output to files rather
than simply taking input and output from the terminal.
Each such command could take special words as arguments indicating
where the output is to go.
It is simpler, and usually
sufficient, to connect these commands to files to which they wish to
write, within the shell itself, and just before they are executed.
Thus suppose we wish to save the current date in a file called `now'.
will print the current date on our terminal.
This is because our terminal is the default
for the date command and the date command prints the date on its
The shell lets us redirect the
`>' and the name of the file where output is to be placed.
command in an environment where its standard output is
the file `now' rather than our terminal.
Thus this command places the current date and time in the file `now'.
It is important to know that the
command was unaware that its output was going to a file rather than
before the command began executing.
One other thing to note here is that the file `now'
need not have existed before the
command was executed; the shell would have created the file if it did
And if the file did exist?
If it had existed previously these previous contents would have been discarded!
exists to prevent this from happening accidentally;
it is discussed in section 2.2.
Metacharacters in the shell
The shell has a large number of
special characters (like `>')
which indicate special functions.
We say that these notations have
In general, most characters which are neither letters nor digits
have special meaning to the shell.
We shall shortly learn a means of
which allows us to create words which contain
and to thus work without constantly worrying about whether certain
characters are metacharacters.
Note that the shell is only reading input when it has prompted with
Thus metacharacters will normally have effect only then.
We need not worry about placing shell metacharacters in a letter
Input from files; pipelines
We learned above how to route the standard output of a command
It is also possible to route the standard input of a command from a file.
This is not often necessary since most commands will read from
a file name given as argument.
command with standard input, where the command normally
for input itself since this is less to type.
We should note that if we just typed
then the sort program would sort lines from its
the standard input, it would sort lines as we typed them on the terminal
until we typed a control-d to generate an end-of-file.
A most useful capability is the ability to combine the standard output
of one command with the standard input of the next, i.e. to run the
commands in a sequence known as a
normally produces a list of the files in our directory with the size
of each in blocks of 512 characters.
If we are interested in learning which of our files is largest we
may wish to have this sorted by size rather than by name, which is
We could look at the many options of
to see if there was an option to do this but would eventually discover
Instead we can use a couple of simple options of the
command, combining it with
option of sort specifies a numeric sort rather than an alphabetic sort.
specifies that the output of the
command run with the option
run with the numeric sort option.
This would give us a sorted list of our files by size, but with the
reverse sort option and the
command in combination with the previous command doing
ls \-s | sort \-n \-r | head \-5
Here we have taken a list of our files sorted alphabetically,
each with the size in blocks.
We have run this to the standard input of the
command asking it to sort numerically in reverse order (largest first).
This output has then been run into the command
which gives us the first few lines out.
In this case we have asked
Thus this command gives us the names and sizes of our 5 largest files.
The metanotation introduced above is called the
Commands separated by `\||\|' characters are connected together by the
shell and the output of each is run into the input of the next.
The leftmost command in a pipeline will normally take its standard
input from the terminal and the rightmost will place its standard
Other examples of pipelines will be given later when we discuss the
one important use of pipes which is illustrated there is in the
routing of information to the line printer.
Many commands to be executed will need the names of files as arguments.
\s-2UNIX\s0 pathnames consist of a number of components separated by `/'.
Each component except the last names a directory in which the next
specifies a file in the directory
which is a subdirectory of the
Within this directory the file named is `motd' which stands
for `message of the day'.
Filenames which do not begin with `/' are interpreted starting at
This directory is, by default, your
directory and can be changed dynamically by the
change directory command.
Most filenames consist of a number of alphanumeric characters and `.'s.
In fact, all printing characters except `/' may appear in filenames.
It is inconvenient to have most non-alphabetic characters in filenames
because many of these have special meaning to the shell.
The character `.' is not a shell-metacharacter and is often used as
prog.c prog.o prog.errs prog.output
(a root portion being that part of the name that is left when a trailing
`.' and following characters which are not `.' are stripped off).
might be the source for a C program,
the file `prog.o' the corresponding object file,
`prog.errs' the errors resulting from a compilation of the program
`prog.output' the output of a run of the program.
If we wished to refer to all four of these files in a command, we could
This word is expanded by the shell, before the command to which it is
an argument is executed, into a list of names which begin with `prog.'.
The character `*' here matches any sequence (including the empty sequence)
of characters in a file name.
The names which match are sorted into the argument list to the command
prog.c prog.errs prog.o prog.output
Note that the names are in lexicographic order here, and a different
order than we listed them above.
command receives four words as arguments, even though we only typed
one word as as argument directly.
The four words were generated by filename expansion of the metasyntax
The character `?' matches any single character in a filename.
will echo a line of filenames; first those with one character names,
then those with two character names, and finally those with three
The names of each length will be independently lexicographically sorted.
Another mechanism consists of a sequence of characters between `[' and `]'.
This metasequence matches any single character from the enclosed set.
We can also place two characters astride a `\-' in this notation to denote
chap.1 chap.2 chap.3 chap.4 chap.5
and otherwise equivalent.
An important point to note is that if a list of argument words to
contains filename expansion syntax, and if this filename expansion syntax
fails to match any existing file names, then the shell considers this
to be an error and prints a diagnostic
Another very important point is that the character `.' at the beginning
of a filename is treated specially.
Neither `*' or `?' or the `[' `]' mechanism will match it.
This prevents accidental matching of the filenames `.' and `..'
in the current directory which have special meaning to the system,
as well as other files such as
We will discuss the special role of the file
Another filename expansion mechanism gives access to the pathname of
directory of other users.
This notation consists of the character `~' followed by another users
For instance the word `~bill' would map to the pathname `/mnt/bill'
if the home directory for `bill' was in the directory `/mnt/bill'.
Since, on large systems, users may have login directories scattered over
many different disk volumes with different prefix directory names,
this notation provides a reliable way of accessing the files
A special case of this notation consists of a `~' alone, e.g. `~/mbox'.
This notation is expanded by the shell into the file `mbox' in your
directory, i.e. into `/mnt/bill/mbox' for me on the Cory Hall \s-2UNIX\s0
This can be very useful if you have used
to change to another users directory and have found a file you wish to
which will be expanded by the shell to
e.g., which the copy command will interpret as a request to make a copy of
in the directory `/mnt/bill'.
The `~' notation doesn't, by itself, force named files to exist.
This is useful, for example, when using the
There also exists a mechanism using the characters `{' and `}' for
abbreviating a set of word which have common parts but cannot
be abbreviated by the above mechanisms because they are not files,
are the names of files which do not yet exist,
are not thus conveniently described.
This mechanism will be described much later,
as it is used much less frequently.
We have already seen a number of metacharacters used by the shell.
These metacharacter pose a problem in that we cannot use them directly
will not echo the character `*'.
It will either echo an sorted list of filenames in the
current directory, or print the message `No match' if there are
no files in the current directory.
The recommended mechanism for placing characters which are neither numbers,
digits, `/', `.' or `\-' in an argument word to a command is to enclose
it with single quotation characters `\'', i.e.
There is one special character `!' which is used by the
mechanism of the shell and which cannot be
It and the character `\'' itself can be preceded by a single `\e'
to prevent their special meaning.
These two mechanisms suffice to place any printing character into a word
which is an argument to a shell command.
When you are running a command from the shell and the shell is
dormant waiting for it to complete there are a couple of ways in
which you can force such a command to complete.
For instance if you type the command
the system will print a copy of a list of all users of the system
This is likely to continue for several minutes unless you stop it.
\s-2DEL\s0 or \s-2RUBOUT\s0
Actually, hitting this key sends this
signal to all programs running on your terminal, including your shell.
The shell normally ignores such signals however, so that the only
does not take any precautions to catch this signal
will cause it to terminate.
has died and prompts you again with `% '.
If you hit \s-2INTERRUPT\s0 again, the shell will just
repeat its prompt since it catches \s-2INTERRUPT\s0 signals
and chooses to continue to execute commands rather than going away
did, which would have the effect of logging you out.
Another way in which many programs terminate is when they get an end-of-file
from their standard input.
program in the first example above was terminated when we hit a control-d
which generates and end-of-file from the standard input.
The shell also terminates when it gets an end-of-file printing `logout';
\s-2UNIX\s0 then logs you off the system.
Since this means that typing too many control-d's can accidentally log us off,
the shell has a mechanism for preventing this.
option will be discussed in section 2.2.
If a command has its standard input redirected from a file, then it will
normally terminate when it reaches the end of this file.
mail bill < prepared.text
the mail command will terminate without our typing a control-d.
This is because it read to the end-of-file of our file
`prepared.text' in which we placed a message for `bill' with an editor.
cat prepared.text \||\| mail bill
command would then have written the text through the pipe to the
standard input of the mail command.
command completed it would have terminated,
closing down the pipeline
command would have received an end-of-file from it and terminated.
Using a pipe here is more complicated than redirecting input
so we would more likely use the first form.
These commands could also have been stopped by sending an \s-2INTERRUPT\s0.
If you write or run programs which are not fully debugged then it may
be necessary to stop them somewhat ungracefully.
This can be done by sending them a \s-2QUIT\s0
signal, generated by a control-\e.
This will usually provoke the shell to produce a message like:
a.out: Quit \-\- Core dumped
`core' has been created containing information about the program `a.out's
You can examine this file yourself, or forward information to the
maintainer of the program telling him/her where the
If you run background commands (as explained in section 2.6) then these
commands will ignore \s-2INTERRUPT\s0 and \s-2QUIT\s0 signals at the
terminal. To stop them you must use the
program. See section 2.6 for an example.
We have so far seen a number of mechanisms of the shell and learned a lot
about the way in which it operates.
The remaining sections will go yet further into the internals of the
shell, but you will surely want to try using the shell before you go any further.
To try it you can log in to \s-2UNIX\s0 and type the following
should be replaced by the name you typed to
`login:' to get onto the system.
Thus I would use `chsh bill /bin/csh'.
You only have to do this once; it takes effect at next login.
You are now ready to try using
Before you do the `chsh' command, the shell you are using when
you log into the system is `/bin/sh'.
In fact, much of the above discussion is applicable to `/bin/sh'.
The next section will introduce many features particular to
so you should change your shell to
before you begin reading it.
Details on the shell for terminal users
Shell startup and termination
When you login, the shell is placed by the system in your
directory and begins by reading commands from a file
All shells which you may create during your terminal session will
We will later see what kinds of commands are usefully placed there.
For now we need not have this file and the shell does not complain about
shell, executed after you login to the system,
will, after it reads commands from
read commands from a file
also in your home directory.
This file contains commands which you wish to do each time you login
to the \s-2UNIX\s0 system.
file looks something like:
on the \s-2CORY\s0 Hall \s-2UNIX\s0 system.
This file contains four commands to be executed by \s-2UNIX\s0
command which informs the system that I usually dial in on a Lear-Siegler
\s-2ADM\-3A\s0 terminal and that if I am on a patchboard port on the
fifth floor of Evans Hall I am probably also on an \s-2ADM\-3A\s0.
in certain ways if I am on a dialup port.
We need not be concerned with exactly what this command does.
In general you may have certain commands in your
which are particular to you.
commands are interpreted directly by the shell and affect the values
of certain shell variables to modify the future behavior of the shell.
tells the shell to print time statistics on commands which take more
than a certain threshold of machine time (in this case 3 \s-2CPU\s0
tells the shell how much history of previous command words it should
save in case I wish to repeat or rerun modified versions of previous
Since there is a certain overhead in this mechanism the shell does
not set this variable by default, but rather lets users who wish to
use the mechanism set it themselves.
The value of 20 is a reasonably large value to assign to
would probably set a value of 5 or 10.
mechanism will be described subsequently.
After executing commands from
the shell reads commands from your terminal, prompting for each with
When it receives an end-of-file from the terminal, the shell
will print `logout' and execute commands from the file `.logout'
After that the shell will die and \s-2UNIX\s0 will log you off the system.
If the system is not going down, you will receive a new login message.
In any case, after the `logout' message the shell is doomed
and will take no further input from the terminal.
The shell maintains a set of
We saw above the variables
which had values `20' and `3'.
In fact, each shell variable has as value an array of
Shell variables may be assigned values by the set command. It has
several forms, the most useful of which was given above and is
Shell variables may be used to store values which are to
be reintroduced into commands later through a substitution mechanism.
The shell variables most commonly referenced are, however, those which the
By changing the values of these variables one can directly affect the
One of the most important variables is the variable
This variable contains a sequence of directory names where the shell
shows the value of all variables currently defined (we usually say
The default value for path will be shown by
This notation indicates that the variable path points to the current
directory `.' and then `/bin' and `/usr/bin'.
Commands which you may write might be in `.' (usually one of
The most heavily used system commands live in `/bin'.
Less heavily used system commands live in `/usr/bin'.
A number of new programs on the system live in the directory
If we wish, as well we might, all shells which we invoke to have
access to these new programs we can place the command
set path=(. /usr/new /bin /usr/bin)
Try doing this and then logging out and back in and do
again to see that the value assigned to
Other useful built in variables are the variable
which shows your home directory,
file to tell the shell not to exit when it receives an end-of-file from
To logout from \s-2UNIX\s0
This is one of several variables which the shell does not care about the
value of, only whether they are
Thus to set this variable you simply do
are built-in commands of the shell.
Finally, some other built-in shell variables of use are the
which redirects the output of a command
will overwrite and destroy the previous contents of the named file.
In this way you may accidentally overwrite a file which is valuable.
If you would prefer that the shell not overwrite files in this
would cause a diagnostic if `now' existed already.
if you really wanted to overwrite the contents of `now'.
The `>!' is a special metasyntax indicating that clobbering the
If you receive mail frequently while you are logged in and wish to
be informed of the arrival of this mail you can put a command
set mail=/usr/mail/yourname
Here you should change `yourname' to your login name.
The shell will look at this file every 10 minutes to see if new mail has arrived.
If you receive mail only infrequently you are better off not
setting this variable. In this case it will only serve to delay the shells
response to you when it checks for mail.
The use of shell variables to introduce text into commands, which is
most useful in shell command scripts, will be introduced in section 2.4.
The shell can maintain a history list into which it places the words
It is possible to use a metanotation to reintroduce commands or words
from commands in forming new commands.
This mechanism can be used to repeat previous commands or to
correct minor typing mistakes in commands.
Consider the following transcript:
michael is on tty0 dialup 300 baud 642-7927
Long time no see michael.
Why don't you call me at 524-4510.
Here we asked the system where
It told us he was on `tty0'
and we told the shell to invoke a `write' command to
This is a history notation which means the last word of the last
command executed, in this case `michael'.
The shell performed this substitution and then echoed the command
Let us assume that we don't hear anything from michael.
was logged in on to see that he had a shell.
Repeating this command via the history substitution `!!'
we saw that he had logged out and that only a
process was running on his terminal.
showed that he was indeed gone, most likely having
hung up the phone in order to be able to call.
This illustrates several useful features of the history mechanism.
The form `!!' repeats the last command execution.
The form `!string' repeats the last command which began with
a word of which `string' is a prefix.
Another useful command form is `\(ualhs\(uarhs' performing a substitute
/mnt/bill/csh/sh..c: No such file or directory
char *pathlist[] = { SRCHP
here we used the substitution to correct a typing mistake,
and then rubbed the command out after we saw that we had found the
The substitution changed the two `.' characters to a single `.'
After this command we might do
cat ~bill/csh/sh.c \||\| lpr
to put a copy of this file on the line printer, or (immediately
pr ~bill/csh/sh.c \||\| lpr
to print a copy on the printer using
More advanced forms of the history mechanism are also possible.
A notion of modification on substitutions allows one to say
(after the first successful
The trailing `:h' on the history substitution here causes only
the head portion of the pathname reintroduced by the history
mechanism to be substituted.
This mechanism and related mechanisms are used less often than
A complete description of history mechanism
features is given in the C shell manual pages in the \s-2UNIX\s0 Programmers Manual.
mechanism which can be used to make transformations on input commands.
This mechanism can be used to simplify the commands you type,
to supply default arguments to commands,
or to perform transformations on commands and their arguments.
The alias facility is similar to the macro facility of many assemblers.
Some of the features obtained by aliasing can be obtained also
using shell command files, but these take place in another instance
of the shell and cannot directly affect the current shells environment
which must be done in the current shell.
As an example, suppose that there is a new version of the mail program
on the system called `Mail'
you wish to use, rather than the standard mail program which is called `mail'.
If you place the shell command
file, the shell will transform an input line of the form
More generally, suppose we wish the command `ls' to always show
sizes of files, that is to always do `\-s'.
creating a new command syntax `dir'
then the shell will translate this to
mechanism can be used to provide short names for commands,
to provide default arguments,
and to define new short commands in terms of other commands.
It is also possible to define aliases which contain multiple
commands or pipelines, showing where the arguments to the original
command are to be substituted using the facilities of the
alias cd \'cd \e!* ; ls \'
command after each change directory
We enclosed the entire alias definition in `\'' characters to prevent
most substitutions from occuring and the character `;' from being
recognized as a parser metacharacter.
The `!' here is escaped with a `\e' to prevent it from being interpreted
when the alias command is typed in.
The `\e!*' here substitutes the entire argument list to the pre-aliasing
command, without giving an error if there were no arguments.
The `;' separating commands is used here
to indicate that one command is to be done and then the next.
alias whois \'grep \e!\(ua /etc/passwd\'
defines a command which looks up its first argument in the password file.
Detached commands; >> and >& redirection
There are a few more metanotations useful to the terminal user
which have not been introduced yet.
The metacharacter `&' may be placed after a command, or after a sequence
of commands separated by `;' or `|'.
This causes the shell to not wait for the commands to terminate before
% pr ~bill/csh/sh.c \||\| lpr &
Here the shell printed two numbers and came back very quickly rather
These numbers are the process numbers assigned by the system to the
\(dgRunning commands in the background like this tends to slow down
the system and is not a good idea if the system is overloaded.
When overloaded, the system will just bog down more if you run a large
number of processes at once.
Since havoc would result if a command run in the background were to
read from your terminal at the same time as the shell does, the
default standard input for a command run in the background is
not your terminal, but an empty file called `/dev/null'.
Commands run in the background are also made immune to
\s-2INTERRUPT\s0 and \s-2QUIT\s0 signals which you may subsequently
generate at your terminal.*
*If a background command stops suddenly when you hit \s-2INTERRUPT\s0
or \s-2QUIT\s0 it is likely a bug in the background program.
If you intend to log off the system before the command completes you
must run the command immune to
This is done by placing the word
`nohup' before each program in the command, i.e.:
nohup man csh \||\| nohup lpr &
In addition to the standard output, commands also have a
which is normally directed to the terminal even when the standard output
is directed to a file or a pipe.
It is occasionally desirable to direct the diagnostic output along with
For instance if you want to redirect the output of a long running command
into a file and wish to have a record of any error diagnostic it produces
The `>&' here tells the shell to route both the diagnostic output and the
standard output into `file'.
Similarly you can give the command
to route both standard and diagnostic output through the pipe
to the line printer daemon
Finally, it is possible to use the form
to place output at the end of an existing file.\(dg
is set, then an error will result if
does not exist, otherwise the shell will create
makes it not be an error for file to not exist when
We now give a few of the useful built-in commands of the shell describing
command described above is used to assign new aliases and to show the
With no arguments it prints the current aliases.
It may also be given an argument such as
to show the current alias for, e.g., `ls'.
commands are equivalent, and change the working directory of the shell.
It is useful to make a directory for each project you wish to work on
and to place all files related to that project in that directory.
Thus after you login you can do
after which you will be in the directory
You can place a group of related files there.
You can return to your `home' login directory by doing just
print working directory command to show the name of the current
The current directory will usually be a subdirectory of your
home directory, and have it (here `/mnt/bill') at the start of it.
command prints its arguments.
It is often used in shell scripts or as an interactive command
to see what filename expansions will yield.
command will show the contents of the history list.
The numbers given with the history events can be used to reference
previous events which are difficult to reference using the
contextual mechanisms introduced above.
There is also a shell variable called
By placing a `!' character in its value the shell will there substitute
the index of the current command in the history list.
You can use this number to refer to this command in a history substitution.
Note that the `!' character had to be escaped here even within `\'' characters.
command can be used to terminate a login shell which has
command can be used to repeat a command several times.
Thus to make 5 copies of the file
command can be used, on version 7 \s-2UNIX\s0 systems,
to set variables in the environment.
will set the value of the environment variable \s-2TERM\s0
exists which will print out the environment.
command can be used to force the current shell to read commands from
can be used after editing in a change to the
file which you wish to take effect before the next time you login.
command can be used to cause a command to be timed no matter how much
\s-2CPU\s0 time it takes.
1200 6300 37650 five.save
command used less that 1/10th of a second of user time and only
3/10th of a second of system time in copying the file `five' to
on the other hand used 1.2 seconds of user time and 0.5 seconds of system
time in 3 seconds of elapsed time in counting the number of words,
character and lines in `five.save'.
The percentage `55%' indicates that over this period of 3 seconds,
our command `wc' used an average of 55 percent of the available \s-2CPU\s0
This is a very high percentage and indicates that the system is lightly
to remove aliases and variable definitions from the shell.
command can be used after starting processes with `&'
to quickly see if they have finished.
If the shell responds immediately with another prompt, they have.
Otherwise you can wait for the shell to prompt at which point they
will have finished, or interrupt the shell by sending a \s-2RUB\s0
or \s-2DELETE\s0 character.
If the shell is interrupted, it will print the names and numbers of the
processes it knows to be unfinished.
% nroff paper \||\| lpr &
You can check again later by doing another
or see which commands are still running by doing a
It is thus counterproductive to run many
commands to see how a background process is doing.\(dg
\(dgIf you do you are usurping with these
commands the processor time the job needs to finish, thereby delaying its
If you run a background process and decide you want to stop it for whatever
You must use the number of the processes you wish to kill.
in the above pipeline you would do
Here the shell printed a diagnostic that we terminated `nroff' only
If we want the shell to discover the termination of all processes
it has created we must, in general, use
This concludes the basic discussion of the shell for terminal users.
There are more features of the shell to be discussed here, and all
features of the shell are discussed in its manual pages.
One useful feature which is discussed later is the
built-in command which can be used to run the same command
sequence with a number of different arguments.
If you intend to use \s-2UNIX\s0 a lot you you should look through
the rest of this document and the shell manual pages to become familiar
with the other facilities which are available to you.
Shell control structures and command scripts
It is possible to place commands in files and to cause shells to be
invoked to read and execute commands from these files,
We here detail those features of the shell useful to the writers of such
It is important to first note what shell scripts are
There is a program called
which is very useful for maintaining a group of related files
or performing sets of operations on related files.
For instance a large program consisting of one or more files
can have its dependencies described in a
which contains definitions of the commands used to create these
different files when changes occur.
Definitions of the means for printing listings, cleaning up the directory
in which the files reside, and installing the resultant programs
are easily, and most appropriately placed in this
This format is superior and preferable to maintaining a group of shell
procedures to maintain these files.
Similarly when working on a document a
may be created which defines how different versions of the document
are to be created and which options of
Invocation and the argv variable
command script may be interpreted by saying
is the name of the file containing a group of
`\&...' is replaced by a sequence of arguments.
The shell places these arguments in the variable
and then begins to read commands from the script.
These parameters are then available through the same mechanisms
which are used to reference any other shell variables.
and place a shell comment at the beginning of the shell script
(i.e. begin the file with a `#' character)
then a `/bin/csh' will automatically be invoked to execute `script' when
If the file does not begin with a `#' then the standard shell
`/bin/sh' will be used to execute it.
This allows you to convert your older shell scripts to use
After each input line is broken into words and history substitutions
are done on it, the input line is parsed into distinct commands.
Before each command is executed a mechanism know as
.I "variable substitution"
Keyed by the character `$' this substitution replaces the names
of variables by their values.
when placed in a command script would cause the current value of the
to be echoed to the output of the shell script.
to be unset at this point.
A number of notations are provided for accessing components and attributes
expands to `1' if name is
It is the fundamental mechanism used for checking whether particular
variables have been assigned values.
All other forms of reference to undefined variables cause errors.
expands to the number of elements in the variable
Undefined variable: argv.
It is also possible to access the components of a variable
which has several values.
gives the first component of
or in the example above `a'.
Other notations useful in shell scripts are
is an integer as a shorthand for
expands to the process number of the current shell.
Since this process number is unique in the system it can
be used in generation of unique temporary file names.
One minor difference between `$\fIn\fR\|' and `$argv[\fIn\fR\|]'
will never yield an out of range subscript error.
This is for compatibility with the way older shells handled parameters.
Another important point is that it is never an error to give a subrange
of the form `n\-'; if there are less than
components of the given variable then no words are substituted.
A range of the form `m\-n' likewise returns an empty vector without giving
an error when \fIm\fR exceeds the number of elements of the given variable,
provided the subscript \fIn\fR is in range.
In order for interesting shell scripts to be constructed it
must be possible to evaluate expressions in the shell based on the
In fact, all the arithmetic operations of the language C are available
with the same precedence that they have in C.
In particular, the operations `==' and `!=' compare strings
and the operators `&&' and `|\|\||' implement the boolean and/or operations.
The shell also allows file enquiries of the form
where `?' is replace by a number of single characters.
For instance the expression primitive
Other primitives test for read, write and execute access to the file,
whether it is a directory, or has non-zero length.
It is possible to test whether a command terminates normally,
form `{ command }' which returns true, i.e. `1' if the command
succeeds exiting normally with exit status 0, or `0' if the command
terminates abnormally or with exit status non-zero.
If more detailed information about the execution status of a command
is required, it can be executed and the variable `$status' examined
Since `$status' is set by every command, it is very transient.
It can be saved if it is inconvenient to use it only in the single
immediately following command.
For a full list of expression components available see the manual
A sample shell script which makes use of the expression mechanism
of the shell and some of its control structure follows:
# Copyc copies those C programs in the specified list
# to the directory ~/backup if they differ from the files
if ($i:r.c != $i) continue # not a .c file so do nothing
if (! \-r ~/backup/$i:t) then
echo $i:t not in backup... not cp\e\'ed
cmp \-s $i ~/backup/$i:t # to set $status
This script makes use of the
command, which causes the shell to execute the commands between the
for each of the values given between `(' and `)' with the named
variable, in this case `i' set to successive values in the list.
Within this loop we may use the command
to stop executing the loop
to prematurely terminate one iteration
loop the iteration variable
has the value at the last iteration.
here to prevent filename expansion of the members of
This is a good idea, in general, if the arguments to a shell script
are filenames which have already been expanded or if the arguments
may contain filename expansion metacharacters.
It is also possible to quote each use of a `$' variable expansion,
but this is harder and less reliable.
The other control construct used here is a statement of the form
\fBif\fR ( expression ) \fBthen\fR
The placement of the keywords here is
flexible due to the current implementation of the shell.\(dg
\(dgThe following two formats are not currently acceptable to the shell:
\fBif\fR ( expression ) # \fBWon't work!\fR
\fBif\fR ( expression ) \fBthen\fR command \fBendif\fR # \fBWon't work\fR
The shell does have another form of the if statement of the form
\fBif\fR ( expression ) \fBcommand\fR
\fBif\fR ( expression ) \e
Here we have escaped the newline for the sake of appearance,
and the `\e' must \fBimmediately\fR.
The command must not involve `\||\|', `&' or `;'
and must not be another control command.
The second form requires the final `\e' to
statements above also admit a sequence of
pairs followed by a single
\fBif\fR ( expression ) \fBthen\fR
\fBelse\fR \fBif\fR (expression ) \fBthen\fR
Another important mechanism used in shell scripts is `:' modifiers.
We can use the modifier `:r' here to extract a root of a filename.
shows how the `:r' modifier strips off the trailing `.bar'.
Other modifiers will take off the last component of a pathname leaving
the head `:h' or all but the last component of a pathname leaving the
These modifiers are fully described in the
manual pages in the programmers manual.
It is also possible to use the
.I "command substitution"
mechanism described in the next major section to perform modifications
on strings to then reenter the shells environment.
Since each usage of this mechanism involves the creation of a new process,
it is much more expensive to use than the `:' modification mechanism.#
#It is also important to note that
the current implementation of the shell limits the number of `:' modifiers
on a `$' substitution to 1.
does not do what one would expect.
Finally, we note that the character `#' lexically introduces a shell
comment in shell scripts (but not from the terminal).
All subsequent characters on the input line after a `#' are discarded
This character can be quoted using `\'' or `\e' to place it in
The shell also has control structures
\fBwhile\fR ( expression )
For details see the manual section for
C programmers should note that we use
A common mistake to make in
statement, with labels looking like they do in C, i.e.:
Supplying input to commands
Commands run from shell scripts receive by default the standard
input of the shell which is running the script.
This it is different from previous shells running
under \s-2UNIX\s0. It allowing shell scripts to fully participate
in pipelines, but mandates extra notation for commands which are to take
Thus we need a metanotation for supplying inline data to commands in
As an example, consider this script which runs the editor to
delete leading blanks from the lines in each argument file
# deblank \-\- remove leading blanks
The notation `<< \'EOF\''
means that the standard input for the
command is to come from the text in the shell script file
up to the next line consisting of exactly `\'EOF\''.
The fact that the `EOF' is enclosed in `\'' characters, i.e. quoted,
causes the shell to not perform variable substitution on the
In general, if any part of the word following the `<<' which the
shell uses to terminate the text to be given to the command is quoted
then these substitutions will not be performed.
In this case since we used the form `1,$' in our editor script
we needed to insure that this `$' was not variable substituted.
We could also have insured this by preceding the `$' here with a `\e',
but quoting the `EOF' terminator is a more reliable way of achieving the
If our shell script creates temporary files, we may wish to catch
interruptions of the shell script so that we can clean up
is a label in our program.
If an interrupt is received the shell will do a
and we can remove the temporary files and then do a
command (which is built in to the shell)
to exit from the shell script.
If we wish to exit with a non-zero status we can do
e.g. to exit with status `1'.
There are other features of the shell useful to writers of shell
command line options can be used to help trace the actions of the shell.
option causes the shell only to read commands and not to execute
them and may sometimes be of use.
One other thing to note is that
will not execute shell scripts which do not begin with the
character `#', that is shell scripts that do not begin with a comment.
Similarly, the `/bin/sh' on your system may well defer to `csh'
to interpret shell scripts which begin with `#'.
This allows shell scripts for both shells to live in harmony.
There is also another quotation mechanism using `"' which allows
only some of the expansion mechanisms we have so far discussed to occur
on the quoted string and serves to make this string into a single word
Miscellaneous, less generally useful, shell mechanisms
Loops at the terminal; variables as vectors
It is occasionally useful to use the
control structure at the terminal to aid in performing a number
For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0
To count the number of persons using each shell one could issue the commands
% grep \-c csh$ /etc/passwd
% grep \-c nsh$ /etc/passwd
% grep \-c \-v sh$ /etc/passwd
Since these commands are very similar we can use
% foreach i (\'sh$\' \'csh$\' \'\-v sh$\')
? grep \-c $i /etc/passwd
Note here that the shell prompts for
input with `? ' when reading the body of the loop.
Very useful with loops are variables which contain lists of filenames
command here gave the variable
a list of all the filenames in the current directory as value.
We can then iterate over these names to perform any chosen function.
The output of a command within `\`' characters is converted by
the shell to a list of words.
You can also place the `\`' quoted string within `"' characters
to take each (non-empty) line as a component of the variable;
preventing the lines from being split into words at blanks and tabs.
A modifier `:x' exists which can be used later to expand each component
of the variable into another variable splitting it into separate words
at embedded blanks and tabs.
Braces { ... } in argument expansion
Another form of filename expansion, alluded
to before involves the characters `{' and `}'.
These characters specify that the contained strings, separated by `,'
are to be consecutively substituted into the containing characters
and the results expanded left to right.
This expansion occurs before the other filename expansions, and may
be applied recursively (i.e. nested).
The results of each expanded string are sorted separately, left
to right order being preserved.
The resulting filenames are not required to exist if no other expansion
This means that this mechanism can be used to generate arguments which are
not filenames, but which have common parts.
A typical use of this would be
mkdir ~/{hdrs,retrofit,csh}
to make subdirectories `hdrs', `retrofit' and `csh'
This mechanism is most useful when the common prefix is longer
than in this example, i.e.
chown bin /usr/{bin/{ex,edit},lib/{ex1.1strings,how_ex}}
A command enclosed in `\`' characters is replaced, just before
filenames are expanded, by the output from that command.
Thus it is possible to do
to save the current directory in the variable
suppling as arguments those files whose names end in `.c'
which have the string `TRACE' in them.*
*Command expansion also occurs in input redirected with `<<'
and within `"' quotations.
Refer to the shell manual section for full details.
Other details not covered here
In particular circumstances it may be necessary to know the exact
nature and order of different substitutions performed by the shell.
The exact meaning of certain combinations of quotations is also
These are detailed fully in its manual section.
The shell has a number of command line option flags mostly of use
in writing \s-2UNIX\s0 programs,
and debugging shell scripts.
See the shells manual section for a list of these options.
Appendix \- Special characters
The following table lists the special characters of
and the \s-2UNIX\s0 system, giving for each the section(s) in which it
A number of these characters also have special meaning in expressions.
; 2.4 separates commands to be executed sequentially
| 1.5 separates commands in a pipeline
( ) 2.2,3.6 brackets expressions and variable values
& 2.5 follows commands to be executed without waiting for completion
/ 1.6 separates components of a file's pathname
\. 1.6 separates root parts of a file name from extensions
? 1.6 expansion character matching any single character
* 1.6 expansion character matching any sequence of characters
[ ] 1.6 expansion sequence matching any single character from a set
~ 1.6 used at the beginning of a filename to indicate home directories
{ } 4.2 used to specify groups of arguments with common parts
\e 1.7 prevents meta-meaning of following single character
\' 1.7 prevents meta-meaning of a group of characters
" 4.3 like \', but allows variable and command expansion
Input/output metacharacters
< 1.3 indicates redirected input
> 1.5 indicates redirected output
Expansion/substitution metacharacters
$ 3.4 indicates variable substitution
! 2.3 indicates history substitution
: 3.6 precedes substitution modifiers
\(ua 2.3 used in special forms of history substitution
\` 4.3 indicates command substitution
# 3.6 begins a shell comment
\- 1.2 prefixes option (flag) arguments to commands
This glossary lists the most important terms introduced in the
shell and gives references to sections of the shell
document for further information about them.
indicate that the command
is in the \s-2UNIX\s0 programmers manual in section 1.
You can get an online copy of its manual page by doing
References of the form (2.5)
indicate that more information can be found in section 2.5 of this
Your current directory has the name `.' as well as the name printed
The current directory `.' is usually the first component of the search
path contained in the variable
thus commands which are in `.' are found first (2.2).
The character `.' is also used in separating components of filenames
The character `.' at the beginning of a component of a pathname is
treated specially and not matched by the filename expansion
metacharacters `?', `*', and `[' `]' pairs (1.6).
Each directory has a file `..' in it which is a reference to its
After changing into the directory with
you can return to the parent directory by doing
The current directory is printed by
specifies a shorter or different name for a \s-2UNIX\s0
command, or a transformation on a command to be performed in
which establishes aliases and can print their current values.
is used to remove aliases (2.6).
Commands in \s-2UNIX\s0 receive a list of argument words.
consists of a command name `echo' and three argument words `a', `b' and `c' (1.1).
The list of arguments to a command written in the shell language
(a shell script or shell procedure) is stored in a variable called
This name is taken from the conventional name in the
C programming language (3.4).
Commands started without waiting for them to complete are called
A directory containing binaries of programs and shell scripts to be
executed is typically called a `bin' directory.
The standard system `bin' directories are `/bin' containing the most
heavily used commands and `/usr/bin' which contains most other user
Other binaries are contained in directories such as `/usr/new' where
You can place binaries in any directory.
If you wish to execute them often, the name of the directories
should be a component of the variable
is a built-in command used to exit from loops within the control
structure of the shell (3.6).
A command executed directly by the shell is called a
Most commands in \s-2UNIX\s0 are not built into the shell,
but rather exist as files in `bin' directories.
These commands are accessible because the directories in which
they reside are named in the
command is used as a label in a
statement in the shells control structure, similar to that of the
Details are given in the shells documentation `csh (NEW)' (3.7).
program catenates a list of specified files on the standard output.
It is usually used to look at the contents of a single file on the terminal,
to `cat a file' (1.8, 2.3).
command is used to change the working directory.
changes your working directory to be your
is usually used because it is easier to type.
command is used to change the shell which you use on \s-2UNIX\s0.
By default, you use an older `standard' version of the shell
which resides in `/bin/sh'.
You can change your shell to `/bin/csh' by doing
chsh your-login-name /bin/csh
It is only necessary to do this once.
The next time you log in to \s-2UNIX\s0 after doing this command,
rather than the shell in `/bin/sh' (1.9).
is a program which compares files.
It is usually used on binary files, or to see if two files are identical (3.6).
For comparing text files the program
described in `diff (1)' is used.
A function performed by the system, either by the shell
(a builtin command) or by a program residing in a file in
a directory within the \s-2UNIX\s0 system is called a
.IP "command substitution"
The replacement of a command enclosed in `\`' characters
by the text output by that command
.I "command substitution"
between `/' characters is called a
which has multiple strings as value is said to have
A builtin command which causes execution of the enclosing
loop to cycle prematurely.
command in the programming language C (3.6).
When a program terminates abnormally, the system places an image
of its current state in a file named `core'.
This `core dump' can be examined with the system debuggers `db (1)'
and `cdb (1)' in order to determine what went wrong with the program (1.8).
If the shell produces a message of the form:
commandname: Illegal instruction \-\- Core dumped
(where `Illegal instruction' is only one of several possible
messages) you should report this to the author of the program
and save the `core' file.
If this was a system program you should report this with the
(copy) program is used to copy the contents of one file into another
It is one of the most commonly used \s-2UNIX\s0 commands (2.6).
directory is read by each shell as it begins execution.
It is usually used to change the setting of the variable
parameters which are to take effect globally (2.1).
command prints the current date and time (1.3).
is the process of correcting mistakes in programs and shell scripts.
The shell has several options and variables which may be used
to aid in shell debugging (4.4).
statements, as it is in the C language
to label the code to be executed if none of the
labels matches the value switched on (3.7).
key on the terminal is used to generate an
which stops the execution of most programs (2.6).
A command run without waiting for it to complete is said to be detached
An error message produced by a program is often referred to as a
Most error messages are not written to the standard output,
since that is often directed away from the terminal (1.3, 1.5).
Error messsages are instead written to the
which may be directed away from the terminal, but usually is not.
Thus diagnostics will usually appear on the terminal (2.5).
A structure which contains files.
At any time you are in one particular directory whose
names can be printed by the command `pwd'.
command will change you to another directory, and make the files
in that directory visible.
The directory in which you are when you first login is your
command prints its arguments (1.6, 2.6, 3.6, 3.10).
command is part of the `if-then-else-endif' control
is generated by the terminal by a control-d,
and whenever a command reads to the end of a file which
it has been given as input.
Commands receiving input from a
receive an end-of-file when the command sending them
Most commands terminate when they receive an end-of-file.
The shell has an option to ignore end-of-file from a terminal
input which may help you keep from logging out accidentally
by typing too many control-d's (1.1, 1.8, 3.8).
A character \e used to prevent the special meaning of a metacharacter
the character from its special meaning.
will echo the character `*' while just
will echo the names of the file in the current directory.
There is also a non-printing character called
Some \s-2UNIX\s0 systems use this character to indicate that
output is to be suspended.
Other systems use control-s.
This file contains information about the accounts currently on the
If consists of a line for each account with fields separated by
You can look at this file by saying
is often used to search for information in this file.
See `passwd (5)' and `grep (1)' for more details.
command is used to force termination of a shell script,
and is built into the shell (3.9).
A command which discovers a problem may reflect this back to the command
(such as a shell) which invoked (executed) it.
It does this by returning a non-zero number as its
a status of zero being considered
command can be used to force a shell command script to give a non-zero
The replacement of strings in the shell input which contain metacharacters
by other strings is referred to as the process of
Thus the replacement of the word `*' by a sorted list of files
in the current directory is a `filename expansion'.
Similarly the replacement of the characters `!!' by the text of
the last command is a `history expansion'.
Expansions are also referred to as
Expressions are used in the shell
to control the conditional structures used in the writing of shell
scripts and in calculating values for these scripts.
The operators available in shell expressions are those of the language
Filenames often consist of a
separated by the character `.'.
By convention, groups of related files often share the same root name.
Thus if `prog.c' were a C program, then the object file for this
program would be stored in `prog.o'.
Similarly a paper written with the
nroff macro package might be stored in
while a formatted version of this paper might be kept in
`paper.out' and a list of spelling errors in
Each file in \s-2UNIX\s0 has a name consisting of up to 14 characters
and not including the character `/' which is used in
Most file names do not begin with the character `.', and contain
only letters and digits with perhaps a `.' separating the root
portion of the filename from an extension (1.6).
Filename expansion uses the metacharacters `*', `?' and `[' and `]'
to provide a convenient mechanism for naming files.
Using filename expansion it is easy to name all the files in
the current directory, or all files which have a common root name.
Other filename expansion mechanisms use the metacharacter `~' and allow
files in other users directories to be named easily (1.6, 4.2).
Many \s-2UNIX\s0 commands accept arguments which are not the names
of files or other users but are used to modify the action of the commands.
options, and by convention consists of one or more letters preceded by
the character `\-' (1.2).
list file commands has an option
`\-s' to list the sizes of files.
command is used in shell scripts and at the terminal to specify
repitition of a sequence of commands while the value of a certain
shell variable ranges through a specified list (3.6, 4.1).
program is part of the system which determines the speed at which
your terminal is to run when you first log in.
It types the initial system banner and `login:'.
When no one is logged in on a terminal a
command shows a command of the form
where `7' here is often some other single letter or digit.
This `7' is an option to the
command, indicating the type of port which it is running on.
command running on a terminal in the output of
you know that no one is logged in on that terminal (2.3).
used in shell scripts to transfer control to a given label (3.7).
command searches through a list of argument files for a specified string.
will print each line in the file
which contains the string `bill'.
in the sense of the editors
`ed (1)' and `ex (1)' (2.3).
`globally find regular expression and print.'
When you hangup a phone line, a \s-2HANGUP\s0 signal
is sent to all running processes on your terminal, causing
them to terminate execution prematurely. If you wish
to start commands to run after you log off a dialup you must
command prints the first few lines of one or more files.
If you have a bunch of files containing text which you are wondering
about it is sometimes is useful to run
with these files as arguments.
This will usually show enough of what is in these files to let you decide
which you are interested in (1.5, 2.3).
mechanism of the shell allows previous commands to be repeated,
possibly after modification to correct typing mistakes or to change
the meaning of the command.
where these commands are kept, and a
variable which controls how large this list is (1.7, 2.6).
Each user has a home directory, which is given in your entry
This is the directory which you are placed in when you first log in.
command with no arguments takes you back to this directory, whose
name is recorded in the shell variable
You can also access the home directories of other users in forming
filenames using a file expansion notation and the character `~' (1.6).
A conditional command within the shell, the
command is used in shell command scripts to make decisions
about what course of action to take next (3.6).
Normally, your shell will exit, printing
if you type a control-d at a prompt of `% '.
This is the way you usually log off the system.
variable if you wish in your
file and then use the command
This is useful if you sometimes accidentally type too many control-d
characters, logging yourself off.
If the system is slow, this can waste much time, as it may
take a long time to log in again (2.2, 2.6).
Many commands on \s-2UNIX\s0 take information from the terminal or from
files which they then act on.
This information is called
Commands normally read for input from their
which is, by default, the terminal.
This standard input can be redirected from a file using a shell metanotation
Many commands will also read from a file specified as argument.
Commands placed in pipelines will read from the output of the previous
The leftmost command in a pipeline reads from the terminal if
you neither redirect its input nor give it a file name to use as
Special mechanisms exist for suppling input to commands in shell
is a signal to a program that is generated by hitting the
\s-2RUBOUT\s0 or \s-2DELETE\s0 key.
It causes most programs to stop execution.
Certain programs such as the shell and the editors
handle an interrupt in special ways, usually by stopping what they
are doing and prompting for another command.
While the shell is executing another command and waiting for it
to finish, the shell does not listen to interrupts.
The shell often wakes up when you hit interrupt because many commands
die when they receive an interrupt (1.8, 2.6, 3.9).
A program which terminates processes run without waiting for them to
directory is read by the shell each time you log in to \s-2UNIX\s0
and the commands there are executed.
There are a number of commands which are usefully placed here
commands to the shell itself (2.1).
command causes a login shell to exit.
Normally, a login shell will exit when you hit control-d
generating an end-of-file, but if you have set
file then this will not work and you must use
to log off the \s-2UNIX\s0 system (2.2).
When you log off of \s-2UNIX\s0 the shell will execute commands from
directory after it prints `logout'.
is the line printer daemon.
is spooled and printed on the \s-2UNIX\s0 line printer.
a list of filenames as arguments to be printed.
as the last component of a
list files command is one of the most commonly used \s-2UNIX\s0
With no argument filenames it prints the names of the files in the
It has a number of useful
arguments, and can also be given the names of directories
as arguments, in which case it lists the names of the files in these
program is used to send and receive messages from other \s-2UNIX\s0
command is used to maintain one or more related files and to
organize functions to be performed on these files.
is easier to use, and more helpful
shell command scripts (3.2).
The file containing command for
The `manual' often referred to is the
`\s-2UNIX\s0 programmers manual.'
It contains a number of sections and a description of each \s-2UNIX\s0
An online version of the manual is accessible through the
Its documentation can be obtained online via
Many characters which are neither letters nor digits have special meaning
either to the shell or to \s-2UNIX\s0.
These characters are called
If it is necessary to place these characters in arguments to commands
without them having their special meaning then they must be
An example of a metacharacter is the character `>' which is used
to indicate placement of output into a file.
most unquoted metacharacters form separate words (1.4).
The appendix to this user's manual lists the metacharacters
in groups by their function.
command is used to create a new directory (2.6).
Substitutions with the history mechanism, keyed by the character `!'
or of variables using the metacharacter `$' are often subjected
to modifications, indicated by placing the character `:' after the
substitution and following this with the modifier itself.
.I "command substitution"
mechanism can also be used to perform modification in a similar way,
but this notation is less clear (3.6).
which may be set in the file
to prevent accidental destruction of files by the `>' output redirection
metasyntax of the shell (2.2, 2.5).
A shell command used to allow background commands to run to completion
even if you log off a dialup before they complete. (2.5)
The standard text formatter on \s-2UNIX\s0 is the program
packages for it, it is possible to have documents automatically
formatted and to prepare them for phototypesetting using the
command is built into the shell and is used to control the action
of a shell command script when an interrupt signal is received (3.9).
Many commands in \s-2UNIX\s0 result in some lines of text which are
This output is usually placed on what is known as the
which is normally connected to the users terminal.
The shell has a syntax using the metacharacter `>' for redirecting
the standard output of a command to a file (1.3).
mechanism and the metacharacter `|' it is also possible for
the standard output of one command to become the standard input of another
Certain commands such as the line printer daemon
do not place their results on the standard output but rather in more
useful places such as on the line printer (2.3).
command places its output on another users terminal rather than its
where they write their error messages.
Normally these go to the terminal even if the standard output has
been sent to a file or another command, but it is possible
to direct error diagnostics along with standard output using
a special metanotation (2.5).
which gives the names of the directories in which it searches for
the commands which it is given.
It always checks first to see if the command it is given is
If it is, then it need not search for the command as it can do it internally.
If the command is not builtin, then the shell searches for a file
with the name given in each of the directories in the
Since the normal definition of the
the shell normally looks in the current directory, and then in
the standard system directories `/bin' and `/usr/bin' for the named
If the command cannot be found the shell will print an error diagnostic.
Scripts of shell commands will be executed using another shell to interpret
them if they have `execute' bits set.
This is normally true because a command of the form
was executed to turn these execute bits on (3.3).
A list of names, separated by `/' characters forms a
between successive `/' characters, names a directory
in which the next component file resides.
Pathnames which begin with the character `/' are interpreted relative
directory in the filesystem.
Other pathnames are interpreted relative to the current directory
The last component of a pathname may name a directory, but
A group of commands which are connected together, the standard
output of each connected to the standard input of the next
mechanism used to connect these commands is indicated by
the shell metacharacter `|' (1.5, 2.3).
command is used to prepare listings of the contents of files
with headers giving the name of the file and the date and
time at which the file was last modified (2.3).
command is used on version 7 \s-2UNIX\s0 systems
to print the current setting of variables in the environment.
As of this writing, only the \s-2VAX/UNIX\s0 system on the fifth
floor of Evans Hall is running a version 7 \s-2UNIX\s0 system.
The other systems are running version 6, which does not have
A instance of a running program is called a process (2.6).
are unique numbers generated for these processes by \s-2UNIX\s0.
commands which can be used to stop background processes. (2.6)
a binary file or shell command script
which performs a useful function is often
See the glossary entry for `manual'.
Many programs will print a prompt on the terminal when they expect
`ex (NEW)' will print a `:' when it expects input.
The shell prompts for input with `% ' and occasionally with `? ' when
reading commands from the terminal (1.1).
which may be set to a different value to change the shells main prompt.
This is mostly used when debugging the shell (2.6).
command is used to show the processes you are currently running.
Each process is shown with its unique process number,
an indication of the terminal name it is attached to,
and the amount of \s-2CPU\s0 time it has used so far.
The command is identified by printing some of the words used
when it was invoked (2.3, 2.6).
Login shells, such as the
you get when you login are shown as `\-'.
command prints the full pathname of the current (working)
generated by a control-\e
is used to terminate programs which are behaving unreasonably.
It normally produces a core image file (1.8).
The process by which metacharacters are prevented their special
meaning, usually by using the character `\' in pairs, or by
using the character `\e' is referred to as
The routing of input or output from or to a file is known
of input or output (1.3).
command iterates another command a specified number of times (2.6).
The \s-2RUBOUT\s0 or \s-2DELETE\s0
key generates an interrupt signal which is used to stop programs
or to cause them to return and prompt for more input (2.6).
Sequences of shell commands placed in a file are called shell command
It is often possible to perform simple tasks using these scripts without
writing a program in a language such as C, by
using the shell to selectively run other programs (3.2, 3.3, 3.10).
command is used to assign new values to shell variables
and to show the values of the current variables.
Many shell variables have special meaning to the shell itself.
Thus by using the set command the behavior of the shell can be affected (2.1).
On version 7 systems variables in the environment `environ (5)'
can be changed by using the
command can be used to print the value of the variables in the environment.
Currently, only the \s-2VAX/UNIX\s0 system on the fifth floor of Evans Hall
is running version 7 \s-2UNIX\s0.
The other systems are running version 6, where
is not necessary and does not exist.
A shell is a command language interpreter.
It is possible to write and run your own shell,
as shells are no different than any other programs as far as the
This manual deals with the details of one particular shell,
program sorts a sequence of lines in ways that can be controlled
command causes the shell to read commands from a specified file.
It is most useful for reading files such as
after changing them (2.6).
A command normally returns a
of zero indicates that the command succeeded.
Commands may return non-zero status to indicate that some abnormal
is set to the status returned by the last command.
It is most useful in shell commmand scripts (3.5, 3.6).
The shell implements a number of
where sequences indicated by metacharacters are replaced by other sequences.
Notable examples of this are history substitution keyed by the
metacharacter `!' and variable substitution indicated by `$'.
We also refer to substitutions as
command of the shell allows the shell
to select one of a number of sequences of commands based on an
statement in the language C (3.7).
When a command which is being executed finished we say it undergoes
Commands normally terminate when they read an end-of-file
from their standard input.
It is also possible to terminate commands by sending them
program terminates specified command whose numbers are given (2.6).
command is part of the shells
`if-then-else-endif' control construct used in command scripts (3.6).
command can be used to measure the amount of \s-2CPU\s0
and real time consumed by a specified command (2.1, 2.6).
program is used to typeset documents.
program is used to set standard erase and kill characters
and to tell the system what kind of terminal you are using.
command removes aliases (2.6).
\s-2UNIX\s0 is an operating system on which
\s-2UNIX\s0 provides facilities which allow
to invoke other programs such as editors and text formatters which
command removes the definitions of shell variables (2.2, 2.6).
hold one or more strings as value.
The most common use of variables is in controlling the behavior
are also used in writing shell programs (shell command scripts)
shell variable can be set to cause commands to be echoed
after they are history expanded.
This is often useful in debugging shell scripts.
variable is set by the shells
command line option (3.10).
causes the shell to pause, and not prompt,
until all commands run in the background have terminated (2.6).
command shows where the users named as arguments are logged into the
builtin control construct is used in shell command scripts (3.7).
A sequence of characters which forms an argument to a command is called
Many characters which are neither letters, digits, `\-', `.' or `/'
form words all by themselves even if they are not surrounded
Any sequence of character may be made into a word by surrounding it
except for the characters `\'' and `!' which require special treatment
This process of placing special characters
in words without their special meaning is called
At an given time you are in one particular directory, called
This directories name is printed by the
command and the files listed by
are the ones in this directory.
You can change working directories using
command is used to communicate with other users who are logged in to