.TH SH 1 "7 February 1983"
sh, for, case, if, while, \fB:\fP, \fB.\fP, break, continue, cd, eval, exec, exit, export, login, read, readonly, set, shift, times, trap, umask, wait \- command language
is a command programming language that executes commands read from a terminal
for the meaning of arguments to the shell.
is a sequence of non blank
separated by blanks (a blank is a
The first word specifies the name of the command to be executed.
Except as specified below the remaining words are passed as arguments
The command name is passed as argument 0 (see
of a simple-command is its exit status
if it terminates normally or 200+\fIstatus\fP if it terminates abnormally (see
for a list of status values).
is a sequence of one or more
The standard output of each command but the last is connected by a
to the standard input of the next command.
Each command is run as a separate process;
the shell waits for the last command to terminate.
is a sequence of one or more
and optionally terminated by
have equal precedence which is lower than that of
also have equal precedence.
A semicolon causes sequential execution; an ampersand causes the preceding
to be executed without waiting for it to finish. The symbol
following to be executed only if the preceding
returns a zero (non zero) value. Newlines may appear in a
instead of semicolons, to delimit commands.
is either a simple-command or one of the following.
The value returned by a command is that of the
last simple-command executed in the command.
\fBfor \fIname\fR \*(OK\fBin \fIword\fR ...\*(CK \fBdo \fIlist \fBdone\fR
is set to the next word in the
is assumed. Execution ends when there are no more words in the list.
\fBcase \fIword \fBin\fR \*(OK\fIpattern \fR\*(OK \fB\(or \fIpattern \fR\*(CK ... \fB) \fIlist \fB;;\fR\*(CK ... \fBesac\fR
associated with the first pattern that matches
The form of the patterns is the same as that used for file name generation.
\fBif \fIlist \fBthen \fIlist\fR \*(OK\fBelif \fIlist \fBthen \fIlist\fR\*(CK ... \*(OK\fBelse \fIlist\fR\*(CK \fBfi\fR
is executed and if it returns zero the
is executed. Otherwise, the
is executed and if its value is zero the
is executed. Failing that the
\fBwhile \fIlist\fR \*(OK\fBdo \fIlist\fR\*(CK \fBdone\fR
command repeatedly executes the
and if its value is zero executes the
otherwise the loop terminates. The value returned by a
command is that of the last executed command in the
to negate the loop termination test.
The following words are only recognized as the first word of a command
if then else elif fi case in esac for while until do done { }
The standard output from a command enclosed in a pair of back quotes
may be used as part or all of a word; trailing newlines are removed.
.B Parameter substitution.
is used to introduce substitutable parameters.
Positional parameters may be assigned values by
Variables may be set by writing
$\fB\|{\fIparameter\fB\|}\fR
is a sequence of letters, digits or underscores (a
a digit, or any of the characters
The value, if any, of the parameter is substituted.
The braces are required only when
is followed by a letter, digit, or underscore
that is not to be interpreted as part of its name. If
is a digit, it is a positional parameter. If
then all the positional parameters, starting with
are substituted separated by spaces.
is set from argument zero when the shell is invoked.
$\fB\|{\fIparameter\|\-word\|\fB}\fR
is set, substitute its value; otherwise substitute
$\fB\|{\fIparameter\|\(eq\|word\|\fB}\fR
the value of the parameter is then substituted.
Positional parameters may not be assigned to in this way.
$\fB\|{\fIparameter\|?\|word\|\fB}\fR
is set, substitute its value; otherwise, print
and exit from the shell. If
is omitted, a standard message is printed.
$\fB\|{\fIparameter\|\(plword\|\fB}\fR
otherwise substitute nothing.
is not evaluated unless it is to be used as the substituted string.
(So that, for example, echo ${d\-\'pwd\'} will only execute
are automatically set by the shell.
The number of positional parameters in decimal.
Options supplied to the shell on invocation or by
The value returned by the last executed command in decimal.
The process number of this shell.
The process number of the last background command invoked.
are used but not set by the shell.
The default argument (home directory) for the
The search path for commands (see
If this variable is set to the name of
a mail file, the shell informs the user of
the arrival of mail in the specified file.
Primary prompt string, by default '$ '.
Secondary prompt string, by default '> '.
Internal field separators, normally
After parameter and command substitution,
any results of substitution are scanned for internal field separator
characters (those found in
and split into distinct arguments where such characters are found.
Explicit null arguments ("" or \'\') are retained.
Implicit null arguments (those resulting from
that have no values) are removed.
Following substitution, each command word is scanned for the characters
If one of these characters appears, the word is regarded as a pattern.
The word is replaced with alphabetically sorted file names that match the
pattern. If no file name is found that matches the pattern,
the word is left unchanged. The character
at the start of a file name or immediately following a
must be matched explicitly.
Matches any string, including the null string.
Matches any single character.
Matches any one of the characters enclosed.
A pair of characters separated by
matches any character lexically between the pair.
The following characters have a special meaning to the shell
and cause termination of a word unless quoted.
\fB; & ( ) \(or \*(LT \*(GT newline space tab\fP
All characters enclosed between a pair of quote marks (\fB\'\|\'\fP),
except a single quote, are quoted. Inside double quotes (\fB"\|"\fP)
parameter and command substitution occurs and
When used interactively, the shell prompts with the value of
before reading a command.
If at any time a newline is typed and further input is needed
to complete a command, the secondary prompt
Before a command is executed its input and output
may be redirected using a special notation interpreted by the shell.
The following may appear anywhere in a simple-command
or may precede or follow a
and are not passed on to the invoked command.
Substitution occurs before
as standard input (file descriptor 0).
as standard output (file descriptor 1).
If the file does not exist, it is created;
otherwise it is truncated to zero length.
If the file exists, output is appended (by seeking to the end);
otherwise the file is created.
The shell input is read up to a line the same as
The resulting document becomes the standard input.
is quoted, no interpretation is placed upon the characters of the document;
otherwise, parameter and command substitution occurs,
is used to quote the characters
and the first character of
The standard input is duplicated from file descriptor
Similarly for the standard output using \*(GT\|.
The standard input is closed.
Similarly for the standard output using \*(GT\|.
If one of the above is preceded by a digit, the
file descriptor created is that specified by the digit
(instead of the default 0 or 1). For example,
creates file descriptor 2 to be a duplicate
If a command is followed by
then the default standard input for the command is the empty file
Otherwise, the environment for the execution of a command contains the
file descriptors of the invoking shell as modified by input
The environment is a list of name-value pairs that is passed to
an executed program in the same way as a normal argument list; see
The shell interacts with the environment in several ways.
On invocation, the shell scans the environment and creates a
for each name found, giving it the corresponding value.
Executed commands inherit the same environment.
If the user modifies the values of these
or creates new ones, none of these affects the environment unless the
command is used to bind the shell's
The environment seen by any executed command is thus composed
of any unmodified name-value pairs originally inherited by the shell,
plus any modifications or additions, all of which must be noted in
may be augmented by prefixing it with one or more assignments to
Thus these two lines are equivalent
(export TERM; TERM=450; cmd args)
keyword arguments are placed in the environment,
even if the occur after the command name.
The following prints 'a=b c' and 'c':
The INTERRUPT and QUIT signals for an invoked
command are ignored if the command is followed by
otherwise signals have the values inherited by the shell from its parent.
Each time a command is executed the above substitutions are carried out.
Except for the 'special commands' listed below a new process is created and
an attempt is made to execute the command via an
defines the search path for the directory containing the command.
Each alternative directory name is separated by a colon
If the command name contains a /, the search path is not used.
Otherwise, each directory in the path is searched for an executable file.
If the file has execute permission but is not an
file, it is assumed to be a file containing shell commands.
A subshell (i.e., a separate process) is spawned to read it.
A parenthesized command is also executed in a subshell.
The following commands are executed in the shell process
and except where specified
no input output redirection is permitted for such commands.
No effect; the command does nothing.
Read and execute commands from
and return. The search path
is used to find the directory containing
\fBbreak\fR \*(OK\fIn\fR\*(CK
\fBcontinue\fR \*(OK\fIn\fR\*(CK
Resume the next iteration of the enclosing
is specified, resume at the
\fBcd\fR \*(OK\fIarg\fR\*(CK
Change the current directory to
\fBeval\fR \*(OK\fIarg \fR...\*(CK
The arguments are read as input to the shell
and the resulting command(s) executed.
\fBexec\fR \*(OK\fIarg \fR...\*(CK
The command specified by the arguments is executed in place of this shell
without creating a new process.
Input output arguments may appear and if no other
arguments are given cause the shell input output to be modified.
\fBexit\fR \*(OK\fIn\fR\*(CK
Causes a non interactive shell to exit with the exit status specified by
is omitted, the exit status is that of the last command executed.
(An end of file will also exit from the shell.)
\fBexport\fR \*(OK\fIname\fR ...\*(CK
The given names are marked for automatic export to the
of subsequently-executed commands.
If no arguments are given, a list of exportable names is printed.
\fBlogin\fR \*(OK\fIarg\fR ...\*(CK
Equivalent to 'exec login arg ...'.
One line is read from the standard input;
successive words of the input are assigned to the variables
in order, with leftover words to the last variable.
The return code is 0 unless the end-of-file is encountered.
\fBreadonly\fR \*(OK\fIname \fR...\*(CK
The given names are marked readonly and
the values of the these names may not be changed
by subsequent assignment.
If no arguments are given, a list of all readonly names is printed.
\fBset\fR \*(OK\fB\-eknptuvx\fR \*(OK\fIarg \fR...\*(CK\*(CK
If non interactive, exit immediately if a command fails.
All keyword arguments are placed in the environment for a command,
not just those that precede the command name.
Read commands but do not execute them.
Exit after reading and executing one command.
Treat unset variables as an error when substituting.
Print shell input lines as they are read.
Print commands and their arguments as they are executed.
These flags can also be used upon invocation of the shell.
The current set of flags may be found in
Remaining arguments are positional
parameters and are assigned, in order, to
etc. If no arguments are given, the values of all names are printed.
The positional parameters from
Print the accumulated user and system times for processes run from the shell.
\fBtrap\fR \*(OK\fIarg\fR\*(CK \*(OK\fIn\fR\*(CK ...
is a command to be read and executed when the shell receives signal(s)
is scanned once when the trap is set and once when the trap is taken.)
Trap commands are executed in order of signal number. If
are reset to their original values.
string, this signal is ignored by the shell and by invoked commands.
is executed on exit from the shell, otherwise upon receipt of signal
with no arguments prints a list of commands associated with each signal number.
\fBumask \fR[ \fInnn\fR ]
The user file creation mask is set to the octal value
is omitted, the current value of the mask is printed.
\fBwait\fP \*(OK\fIn\fP\*(CK
Wait for the specified process and report its termination status. If
is not given, all currently active child processes are waited for.
The return code from this command is that of the process waited for.
If the first character of argument zero is
.BR \s-2$HOME\s0/.\|profile ,
Commands are then read as described below.
The following flags are interpreted by the shell when it is invoked.
flag is present, commands are read from
flag is present or if no arguments remain
then commands are read from the standard input.
Shell output is written to file descriptor 2.
if the shell input and output are attached to a terminal (as told by
In this case the terminate signal SIGTERM (see
is ignored (so that 'kill 0'
does not kill an interactive shell) and the interrupt signal
SIGINT is caught and ignored (so that
In all cases SIGQUIT is ignored by the shell.
The remaining flags and arguments are described under the
Errors detected by the shell, such as syntax errors cause the shell
to return a non zero exit status.
If the shell is being used non interactively
then execution of the shell file is abandoned.
Otherwise, the shell returns the exit status of
the last command executed (see also
If \*(LT\*(LT is used to provide standard input to an asynchronous
process invoked by &, the shell gets mixed up about naming the input document.
A garbage file /tmp/sh* is created, and the shell complains about
not being able to find the file by another name.