.\" @(#)t1 6.1 (Berkeley) %G%
.EH 'USD:3-%''An Introduction to the UNIX Shell'
.OH 'An Introduction to the UNIX Shell''USD:3-%'
An Introduction to the UNIX Shell
(Updated for 4.3BSD by Mark Seiden)
\(dd This paper describes sh(1). If it's the c shell (csh) you're interested in,
a good place to begin is William Joy's paper "An Introduction to the C shell" (USD:4).
is a command programming language that provides an interface
control-flow primitives, parameter passing, variables and
while, if then else, case
Two-way communication is possible between the
String-valued parameters, typically file names or flags, may be
A return code is set by commands that may be used to determine control-flow,
and the standard output from a command may be used
can modify the environment
Input and output can be redirected
to files, and processes that communicate through `pipes'
sequence that can be defined by the user.
Commands can be read either from the terminal or from a file,
which allows command procedures to be
.ds ST \v'.3m'\s+2*\s0\v'-.3m'
The shell is both a command language
and a programming language
that provides an interface to the UNIX
This memorandum describes, with
examples, the UNIX shell.
The first section covers most of the
Some familiarity with UNIX
is an advantage when reading this section;
Section 2 describes those features
of the shell primarily intended
for use within shell procedures.
These include the control-flow
primitives and string-valued variables
A knowledge of a programming language
would be a help when reading this section.
The last section describes the more
advanced features of the shell.
References of the form "see \fIpipe\fP (2)"
are to a section of the UNIX manual.
seventh 1978 ritchie thompson
Simple commands consist of one or more words
The first word is the name of the command
to be executed; any remaining words
are passed as arguments to the command.
is a command that prints the names
prints a list of files in the current
The argument \fI\(mil\fP tells \fIls\fP
to print status information, size and
the creation date for each file.
1.2\ Background\ commands
To execute a command the shell normally
creates a new \fIprocess\fP
and waits for it to finish.
A command may be run without waiting
calls the C compiler to compile
The trailing \fB&\fP is an operator that instructs the shell
not to wait for the command to finish.
To help keep track of such a process
the shell reports its process
number following its creation.
A list of currently active processes may be obtained
using the \fIps\fP command.
1.3\ Input\ output\ redirection
Most commands produce output on the standard output
that is initially connected to the terminal.
This output may be sent to a file
is interpreted by the shell and is not passed
as an argument to \fIls.\fP
If \fIfile\fP does not exist then the
otherwise the original contents of
\fIfile\fP are replaced with the output
Output may be appended to a file
In this case \fIfile\fP is also created if it does not already
The standard input of a command may be taken
from a file instead of the terminal by
The command \fIwc\fP reads its standard input
(in this case redirected from \fIfile\fP)
and prints the number of characters, words and
If only the number of lines is required
1.4\ Pipelines\ and\ filters
The standard output of one command may be
connected to the standard input of another
Two commands connected in this way constitute
the overall effect is the same as
except that no \fIfile\fP is used.
Instead the two processes are connected
by a pipe (see \fIpipe\fP (2)) and are
Pipes are unidirectional and
synchronization is achieved by
halting \fIwc\fP when there is
nothing to read and halting \fIls\fP
A \fIfilter\fP is a command
that reads its standard input,
transforms it in some way,
and prints the result as output.
One such filter, \fIgrep,\fP
selects from its input those lines
that contain some specified string.
prints those lines, if any, of the output
from \fIls\fP that contain
Another useful filter is \fIsort\fP.
will print an alphabetically sorted list
A pipeline may consist of more than two commands,
ls \*(VT grep old \*(VT wc \(mil
prints the number of file names
in the current directory containing
1.5\ File\ name\ generation
Many commands accept arguments
prints information relating to the file \fImain.c\fP\|.
The shell provides a mechanism
for generating a list of file names
generates, as arguments to \fIls,\fP
all file names in the current directory that end in \fI.c\|.\fP
The character \*(ST is a pattern that will match any string
including the null string.
In general \fIpatterns\fP are specified
Matches any string of characters
including the null string.
Matches any single character.
Matches any one of the characters
A pair of characters separated by a minus will
match any character lexically between
matches all names in the current directory
one of the letters \fIa\fP through \fIz.\fP
matches all names in the directory
\fB/usr/fred/test\fP that consist of a single character.
If no file name is found that matches
the pattern then the pattern is passed,
unchanged, as an argument.
This mechanism is useful both to save typing
and to select names according to some pattern.
It may also be used to find files.
echo /usr/fred/\*(ST/core
finds and prints the names of all \fIcore\fP files in sub-directories
(\fIecho\fP is a standard UNIX command that prints
its arguments, separated by blanks.)
This last feature can be expensive,
sub-directories of \fB/usr/fred\|.\fP
There is one exception to the general
rules given for patterns.
at the start of a file name must be explicitly
will therefore echo all file names in the current
will echo all those file names that begin with `\fB.\fP'\|.
This avoids inadvertent matching
of the names `\fB.\fP' and `\fB..\fP'
which mean `the current directory'
and `the parent directory'
(Notice that \fIls\fP suppresses
information for the files `\fB.\fP' and `\fB..\fP'\|.)
Characters that have a special meaning
to the shell, such as \fB< > \*(ST ? \*(VT &\|,\fR
are called metacharacters.
A complete list of metacharacters is given
Any character preceded by a \fB\\\fR is \fIquoted\fP
and loses its special meaning, if any.
The \fB\\\fP is elided so that
will echo a single \fB?\|,\fP
will echo a single \fB\\\|.\fR
To allow long strings to be continued over
the sequence \fB\\newline\fP
\fB\\\fP is convenient for quoting
When more than one character needs
quoting the above mechanism is clumsy and
A string of characters may be quoted
by enclosing the string between single quotes.
echo xx\'\*(ST\*(ST\*(ST\*(ST\'xx
The quoted string may not contain
but may contain newlines, which are preserved.
This quoting mechanism is the most
simple and is recommended
A third quoting mechanism using double quotes
that prevents interpretation of some but not all
When the shell is used from a terminal it will
issue a prompt before reading a command.
By default this prompt is `\fB$\ \fR'\|.
It may be changed by saying,
that sets the prompt to be the string \fIyesdear\|.\fP
If a newline is typed and further input is needed
then the shell will issue the prompt `\fB>\ \fR'\|.
Sometimes this can be caused by mistyping
If it is unexpected then an interrupt (\s-1DEL\s0)
will return the shell to read another command.
This prompt may be changed by saying, for example,
1.8\ The\ shell\ and\ login
Following \fIlogin\fP (1)
the shell is called to read and execute
commands typed at the terminal.
If the user's login directory
contains the file \fB.profile\fP
then it is assumed to contain commands
and is read by the shell before reading
any commands from the terminal.
Print the names of files in the current directory.
Put the output from \fIls\fP into \fIfile.\fP
Print the number of files in the current directory.
Print those file names containing the string \fIold.\fP
\fBls \*(VT grep old \*(VT wc \(mil\fR
Print the number of files whose name contains the string \fIold.\fP
Run \fIcc\fP in the background.