.\" $Copyright:	$
.\" Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
.\" Sequent Computer Systems, Inc.   All rights reserved.
.\"  
.\" This software is furnished under a license and may be used
.\" only in accordance with the terms of that license and with the
.\" inclusion of the above copyright notice.   This software may not
.\" be provided or otherwise made available to, or used by, any
.\" other person.  No title to or ownership of the software is
.\" hereby transferred.
...
. \" $Header: t1 1.3 86/03/06 $
.RP
.TL 
An Introduction to the UNIX Shell
.AU
S. R. Bourne
.AI
.MH
.AB
.LP
The
.ul
shell
is a command programming language that provides an interface
to the
.UX
operating system.
Its features include
control-flow primitives, parameter passing, variables and
string substitution.
Constructs such as
.ul
while, if then else, case
and
.ul
for
are available.
Two-way communication is possible between the
.ul
shell
and commands.
String-valued parameters, typically file names or flags, may be
passed to a command.
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
as shell input.
.LP
The
.ul
shell
can modify the environment
in which commands run.
Input and output can be redirected
to files, and processes that communicate through `pipes'
can be invoked.
Commands are found by
searching directories
in the file system in a
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
stored for later use.
.AE
.ds ST \v'.3m'\s+2*\s0\v'-.3m'
.SH
1.0\ Introduction
.LP
The shell is both a command language
and a programming language
that provides an interface to the UNIX
operating system.
This memorandum describes, with
examples, the UNIX shell.
The first section covers most of the
everyday requirements
of terminal users.
Some familiarity with UNIX
is an advantage when reading this section;
see, for example,
"UNIX for beginners".
.[
unix beginn kernigh 1978
.]
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
provided by the shell.
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 \f2pipe\fP (2)"
are to a section of the UNIX manual.
.[
seventh 1978 ritchie thompson
.]
.SH
1.1\ Simple\ commands
.LP
Simple commands consist of one or more words
separated by blanks.
The first word is the name of the command
to be executed; any remaining words
are passed as arguments to the command.
For example,
.DS
	who
.DE
is a command that prints the names
of users logged in.
The command
.DS
	ls \(mil
.DE
prints a list of files in the current
directory.
The argument \f2\(mil\fP tells \f2ls\fP
to print status information, size and
the creation date for each file.
.SH
1.2\ Background\ commands
.LP
To execute a command the shell normally
creates a new \f2process\fP
and waits for it to finish.
A command may be run without waiting
for it to finish.
For example,
.DS
	cc pgm.c &
.DE
calls the C compiler to compile
the file \f2pgm.c\|.\fP
The trailing \f3&\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 \f2ps\fP command.
.SH
1.3\ Input\ output\ redirection
.LP
Most commands produce output on the standard output
that is initially connected to the terminal.
This output may be sent to a file
by writing, for example,
.DS
	ls \(mil >file
.DE
The notation \f2>file\fP
is interpreted by the shell and is not passed
as an argument to \f2ls.\fP
If \f2file\fP does not exist then the
shell creates it;
otherwise the original contents of
\f2file\fP are replaced with the output
from \f2ls.\fP
Output may be appended to a file
using the notation
.DS
	ls \(mil \*(APfile
.DE
In this case \f2file\fP is also created if it does not already
exist.
.LP
The standard input of a command may be taken
from a file instead of the terminal by
writing, for example,
.DS
	wc <file
.DE
The command \f2wc\fP reads its standard input
(in this case redirected from \f2file\fP)
and prints the number of characters, words and
lines found.
If only the number of lines is required
then
.DS
	wc \(mil <file
.DE
could be used.
.SH
1.4\ Pipelines\ and\ filters
.LP
The standard output of one command may be
connected to the standard input of another
by writing
the `pipe' operator,
indicated by \*(VT,
as in,
.DS
	ls \(mil \*(VT wc
.DE
Two commands connected in this way constitute
a \f2pipeline\fP and
the overall effect is the same as
.DS
	ls \(mil >file; wc <file
.DE
except that no \f2file\fP is used.
Instead the two processes are connected
by a pipe (see \f2pipe\fP (2)) and are
run in parallel.
Pipes are unidirectional and
synchronization is achieved by
halting \f2wc\fP when there is
nothing to read and halting \f2ls\fP
when the pipe is full.
.LP
A \f2filter\fP is a command
that reads its standard input,
transforms it in some way,
and prints the result as output.
One such filter, \f2grep,\fP
selects from its input those lines
that contain some specified string.
For example,
.DS
	ls \*(VT grep old
.DE
prints those lines, if any, of the output
from \f2ls\fP that contain
the string \f2old.\fP
Another useful filter is \f2sort\fP.
For example,
.DS
	who \*(VT sort
.DE
will print an alphabetically sorted list
of logged in users.
.LP
A pipeline may consist of more than two commands,
for example,
.DS
	ls \*(VT grep old \*(VT wc \(mil
.DE
prints the number of file names
in the current directory containing
the string \f2old.\fP
.SH
1.5\ File\ name\ generation
.LP
Many commands accept arguments
which are file names.
For example,
.DS
	ls \(mil main.c
.DE
prints information relating to the file \f2main.c\fP\|.
.LP
The shell provides a mechanism
for generating a list of file names
that match a pattern.
For example,
.DS
	ls \(mil \*(ST.c
.DE
generates, as arguments to \f2ls,\fP
all file names in the current directory that end in \f2.c\|.\fP
The character \*(ST is a pattern that will match any string
including the null string.
In general \f2patterns\fP are specified
as follows.
.RS
.IP \f3\*(ST\f1 8
Matches any string of characters
including the null string.
.IP \f3?\f1 8
Matches any single character.
.IP \f3[\*(ZZ]\f1 8
Matches any one of the characters
enclosed.
A pair of characters separated by a minus will
match any character lexically between
the pair.
.RE
.LP
For example,
.DS
	[a\(miz]\*(ST
.DE
matches all names in the current directory
beginning with
one of the letters \f2a\fP through \f2z.\fP
.DS
	/usr/fred/test/?
.DE
matches all names in the directory
\f3/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.
.LP
This mechanism is useful both to save typing
and to select names according to some pattern.
It may also be used to find files.
For example,
.DS
	echo /usr/fred/\*(ST/core
.DE
finds and prints the names of all \f2core\fP files in sub-directories
of \f3/usr/fred\|.\fP
(\f2echo\fP is a standard UNIX command that prints
its arguments, separated by blanks.)
This last feature can be expensive,
requiring a scan of all
sub-directories of \f3/usr/fred\|.\fP
.LP
There is one exception to the general
rules given for patterns.
The character `\f3.\fP'
at the start of a file name must be explicitly
matched.
.DS
	echo \*(ST
.DE
will therefore echo all file names in the current
directory not beginning
with `\f3.\fP'\|.
.DS
	echo \f3.\fP\*(ST
.DE
will echo all those file names that begin with `\f3.\fP'\|.
This avoids inadvertent matching
of the names `\f3.\fP' and `\f3..\fP'
which mean `the current directory'
and `the parent directory'
respectively.
(Notice that \f2ls\fP suppresses
information for the files `\f3.\fP' and `\f3..\fP'\|.)
.SH
1.6\ Quoting
.LP
Characters that have a special meaning
to the shell, such as \f3< > \*(ST ? \*(VT &\|,\f1
are called metacharacters.
A complete list of metacharacters is given
in appendix B.
Any character preceded by a \f3\\\f1 is \f2quoted\fP
and loses its special meaning, if any.
The \f3\\\fP is elided so that
.DS
	echo \\\\?
.DE
will echo a single \f3?\|,\fP
and
.DS
	echo \\\\\\\\
.DE
will echo a single \f3\\\|.\f1
To allow long strings to be continued over
more than one line
the sequence \f3\\newline\fP
is ignored.
.LP
\f3\\\fP is convenient for quoting
single characters.
When more than one character needs
quoting the above mechanism is clumsy and
error prone.
A string of characters may be quoted
by enclosing the string between single quotes.
For example,
.DS
	echo xx\'\*(ST\*(ST\*(ST\*(ST\'xx
.DE
will echo
.DS
	xx\*(ST\*(ST\*(ST\*(STxx
.DE
The quoted string may not contain
a single quote
but may contain newlines, which are preserved.
This quoting mechanism is the most
simple and is recommended
for casual use.
.LP
A third quoting mechanism using double quotes
is also available
that prevents interpretation of some but not all
metacharacters.
Discussion of the
details is deferred
to section 3.4\|.
.SH
1.7\ Prompting
.LP
When the shell is used from a terminal it will
issue a prompt before reading a command.
By default this prompt is `\f3$\ \f1'\|.
It may be changed by saying,
for example,
.DS
	\s-1PS1\s0=yesdear
.DE
that sets the prompt to be the string \f2yesdear\|.\fP
If a newline is typed and further input is needed
then the shell will issue the prompt `\f3>\ \f1'\|.
Sometimes this can be caused by mistyping
a quote mark.
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,
.DS
	\s-1PS2\s0=more
.DE
.SH
1.8\ The\ shell\ and\ login
.LP
Following \f2login\fP (1)
the shell is called to read and execute
commands typed at the terminal.
If the user's login directory
contains the file \f3.profile\fP
then it is assumed to contain commands
and is read by the shell before reading
any commands from the terminal.
.SH
1.9\ Summary
.sp
.RS
.IP \(bu
\f3ls\fP
.br
Print the names of files in the current directory.
.IP \(bu
\f3ls >file\fP
.br
Put the output from \f2ls\fP into \f2file.\fP
.IP \(bu
\f3ls \*(VT wc \(mil\f1
.br
Print the number of files in the current directory.
.IP \(bu
\f3ls \*(VT grep old\f1
.br
Print those file names containing the string \f2old.\fP
.IP \(bu
\f3ls \*(VT grep old \*(VT wc \(mil\f1
.br
Print the number of files whose name contains the string \f2old.\fP
.IP \(bu
\f3cc pgm.c &\f1
.br
Run \f2cc\fP in the background.
.RE
