Sh is the standard command interpreter for the system.
The current version of sh is in the process of being changed to
conform with the POSIX 1003.2 and 1003.2a specifications for
the shell. This version has many features which make it appear
similar in some respects to the Korn shell, but it is not a Korn
shell clone (run GNU's bash if you want that). Only features
designated by POSIX, plus a few Berkeley extensions, are being
incorporated into this shell. We expect POSIX conformance by the
time 4.4 BSD is released.
This man page is not intended to be a tutorial or a complete
specification of the shell.
.sp2
.BOverview
.sp
.LP
The shell is a command that reads lines from
either a file or the terminal, interprets them, and
generally executes other commands. It is the program that is running
when a user logs into the system (although a user can select
a different shell with the chsh(1) command).
The shell
implements a language that has flow control constructs,
a macro facility that provides a variety of features in
addition to data storage, along with built in history and line
editing capabilities. It incorporates many features to
aid interactive use and has the advantage that the interpretative
language is common to both interactive and non-interactive
use (shell scripts). That is, commands can be typed directly
to the running shell or can be put into a file and the file
can be executed directly by the shell.
.sp2
.BInvocation
.sp
.LP
If no args are present and if the standard input of the shell
is connected to a terminal (or if the -i flag is set), the shell
is considered an interactive shell. An interactive shell
generally prompts before each command and handles programming
and command errors differently (as described below).
When first starting, the shell inspects argument 0, and
if it begins with a dash '-', the shell is also considered
a login shell. This is normally done automatically by the system
when the user first logs in. A login shell first reads commands
from the files /etc/profile and .profile if they exist.
If the environment variable ENV is set on entry to a shell,
or is set in the .profile of a login shell, the shell next reads
commands from the file named in ENV. Therefore, a user should
place commands that are to be executed only at login time in
the .profile file, and commands that are executed for every
shell inside the ENV file. To set the ENV variable to some
file, place the following line in your .profile of your home
directory
.nf
ENV=$HOME/.shinit; export ENV
.fi
substituting for ``.shinit'' any filename you wish.
Since the ENV file is read for
every invocation of the shell, including shell scripts and
non-interactive shells, the following paradigm is useful
for restricting commands in the ENV file to interactive invocations.
Place commands within the ``case'' and ``esac'' below (these
commands are described later):
.nf
case $- in *i*)
# commands for interactive use only
...
esac
.fi
If command line arguments besides the options have been
specified, then the shell treats the first argument as the
name of a file from which to read commands (a shell script), and
the remaining arguments are set as the positional parameters
of the shell ($1, $2, etc). Otherwise, the shell reads commands
from its standard input.
.sp2
.BArgumentListProcessing
.sp
.LP
All of the single letter options have a corresponding name
that can be used as an argument to the '-o' option. The
set -o name is provided next to the single letter option in
the description below.
Specifying a dash ``-'' turns the option on, while using a plus ``+''
disables the option.
The following options can be set from the command line or
with the set(1) builtin (described later).
.TP
-a allexport
Export all variables assigned to.
(UNIMPLEMENTED for 4.4alpha)
.TP
-C noclobber
Don't overwrite existing files with ``>''.
(UNIMPLEMENTED for 4.4alpha)
.TP
-e errexit
If not interactive, exit immediately if any
untested command fails.
The exit status of a command is considered to be
explicitly tested if the command is used to control
an if, elif, while, or until; or if the command is the left
hand operand of an ``&&'' or ``||'' operator.
.TP
-f noglob
Disable pathname expansion.
.TP
-n noexec
If not interactive, read commands but do not
execute them. This is useful for checking the
syntax of shell scripts.
.TP
-u nounset
Write a message to standard error when attempting
to expand a variable that is not set, and if the
shell is not interactive, exit immediately.
(UNIMPLEMENTED for 4.4alpha)
.TP
-v verbose
The shell writes its input to standard error
as it is read. Useful for debugging.
.TP
-x xtrace
Write each command to standard error (preceded
by a '+ ') before it is executed. Useful for
debugging.
.TP
-I ignoreeof
Ignore EOF's from input when interactive.
.TP
-i interactive
Force the shell to behave interactively.
.TP
-m monitor
Turn on job control (set automatically when
interactive).
.TP
-s stdin
Read commands from standard input (set automatically
if no file arguments are present). This option has
no effect when set after the shell has already started
running (i.e. with set(1)).
.TP
-V vi
Enable the builtin vi(1) command line editor (disables
-E if it has been set).
.TP
-E emacs
Enable the builtin emacs(1) command line editor (disables
-V if it has been set).
.TP
-b notify
Enable asynchronous notification of background job
completion.
(UNIMPLEMENTED for 4.4alpha)
.LP
.sp2
.BLexicalStructure
.sp
.LP
The shell reads input in terms of lines from a file and breaks
it up into words at whitespace (blanks and tabs), and at
certain sequences of
characters that are special to the shell called ``operators''.
There are two types of operators: control operators and
redirection operators (their meaning is discussed later).
Following is a list of operators:
.nf
.sp
Control operators: & && ( ) ; ;; | || <newline>
.sp
Redirection operator: < > >| << >> <& >& <<- <>
.sp
.fi
.sp2
.BQuoting
.sp
.LP
Quoting is used to remove the special meaning of certain characters
or words to the shell, such as operators, whitespace, or
keywords. There are three types of quoting: matched single quotes,
matched double quotes, and backslash.
.sp2
.BBackslash
.sp
.LP
A backslash preserves the literal meaning of the following
character, with the exception of <newline>. A backslash preceding
a <newline> is treated as a line continuation.
.sp2
.BSingleQuotes
.sp
.LP
Enclosing characters in single quotes preserves the literal
meaning of all the characters.
.sp2
.BDoubleQuotes
.sp
.LP
Enclosing characters within double quotes preserves the literal
meaning of all characters except dollarsign ($), backquote (`),
and backslash (\\). The backslash inside double quotes is
historically weird, and serves to quote only the following
characters: $ ` " \\ <newline>.
Otherwise it remains literal.
.sp2
.BReservedWords
.sp
.LP
Reserved words are words that have special meaning to the
shell and are recognized at the beginning of a line and
after a control operator. The following are reserved words:
.nf
! elif fi while case
else for then { }
do done until if esac
.fi
Their meaning is discussed later.
.sp2
.BAliases
.sp
.LP
An alias is a name and corresponding value set using the alias(1)
builtin command. Whenever a reserved word may occur (see above),
and after checking for reserved words, the shell
checks the word to see if it matches an alias. If it does,
it replaces it in the input stream with its value. For example,
if there is an alias called ``lf'' with the value ``ls -F'',
then the input
.nf
lf foobar <return>
would become
ls -F foobar <return>
.fi
.LP
Aliases provide a convenient way for naive users to
create shorthands for commands without having to learn how
to create functions with arguments. They can also be
used to create lexically obscure code. This use is discouraged.
.sp2
.BCommands
.sp
.LP
The shell interprets the words it reads according to a
language, the specification of which is outside the scope
of this man page (refer to the BNF in the POSIX 1003.2
document). Essentially though, a line is read and if
the first word of the line (or after a control operator)
is not a reserved word, then the shell has recognized a
simple command. Otherwise, a complex command or some
other special construct may have been recognized.
.sp2
.BSimpleCommands
.sp
.LP
If a simple command has been recognized, the shell performs
the following actions:
.sp
1) Leading words of the form ``name=value'' are
stripped off and assigned to the environment of
the simple command. Redirection operators and
their arguments (as described below) are stripped
off and saved for processing.
.sp
2) The remaining words are expanded as described in
the section called ``Expansions'', and the
first remaining word is considered the command
name and the command is located. The remaining
words are considered the arguments of the command.
If no command name resulted, then the ``name=value''
variable assignments recognized in 1) affect the
current shell.
.sp
3) Redirections are performed as described in
the next section.
.sp2
.BRedirections
.sp
.LP
Redirections are used to change where a command reads its input
or sends its output. In general, redirections open, close, or
duplicate an existing reference to a file. The overall format
used for redirection is:
.nf
[n] redir-op file
.fi
where redir-op is one of the redirection operators mentioned
previously. Following is a list of the possible redirections.
The [n] is an optional number, as in '3' (not '[3]'), that
refers to a file descriptor.
.TP
[n]> file
Redirect standard output (or n) to file.
.TP
[n]>| file
Same, but override the -C option.
.TP
[n]>> file
Append standard output (or n) to file.
.TP
[n]< file
Redirect standard input (or n) from file.
.TP
[n1]<&n2
Duplicate standard input (or n1) from
file descriptor n2.
.TP
[n]<&-
Close standard input (or n).
.TP
[n1]>&n2
Duplicate standard output (or n) from
n2.
.TP
[n]>&-
Close standard output (or n).
.TP
[n]<> file
Open file for reading and writing on
standard input (or n).
.LP
The following redirection is often called a ``here-document''.
.nf
[n]<< delimiter
here-doc-text...
delimiter
.fi
All the text on successive lines up to the delimiter is
saved away and made available to the command on standard
input, or file descriptor n if it is specified. If the delimiter
as specified on the initial line is quoted, then the here-doc-text
is treated literally, otherwise the text is subjected to
parameter expansion, command substitution, and arithmetic
expansion (as described in the section on ``Expansions''). If
the operator is ``<<-'' instead of ``<<'', then leading tabs
in the here-doc-text are stripped.
.sp2
.BSearchandExecution
.sp
.LP
There are three types of commands: shell functions, builtin commands, and normal programs -- and the
command is searched for (by name) in that order. They
each are executed in a different way.
.LP
When a shell function is executed, all of the shell positional parameters (except $0, which remains unchanged) are
set to the arguments of the shell function.
The variables which are explicitly placed in the environment of
the command (by placing assignments to them before the
function name) are made local to the function and are set
to the values given. Then the command given in the function
definition is executed. The positional parameters are
restored to their original values when the command completes.
.LP
Shell builtins are executed internally to the shell, without spawning a new process.
.LP
Otherwise, if the command name doesn't match a function
or builtin, the command is searched for as a normal
program in the filesystem (as described in the next section).
When a normal program is executed, the shell runs the program,
passing the arguments and the environment to the
program. If the program is a shell procedure, the shell
will interpret the program in a subshell. The shell will
reinitialize itself in this case, so that the effect will
be as if a new shell had been invoked to handle the shell
procedure, except that the location of commands located in
the parent shell will be remembered by the child.
.sp2
.BPathSearch
.sp
.LP
When locating a command, the shell first looks to see if
it has a shell function by that name. Then it looks for a
builtin command by that name.
Finally, it searches each
entry in PATH in turn for the command.
.LP
The value of the PATH variable should be a series of
entries separated by colons. Each entry consists of a
directory name.
The current directory
may be indicated by an empty directory name.
.LP
Command names containing a slash are simply executed without performing any of the above searches.
.sp2
.BCommandExitStatus
.sp
.LP
Each command has an exit status that can influence the behavior
of other shell commands. The paradigm is that a command exits
with zero for normal or success, and non-zero for failure,
error, or a false indication. The man page for each command
should indicate the various exit codes and what they mean.
Additionally, the builtin commands return exit codes, as does
an executed function.
.sp2
.BComplexCommands
.sp
.LP
Complex commands are combinations of simple commands
with control operators or reserved words, together creating a larger complex
command. More generally, a command is one of the following:
.nf
- simple command
- pipeline
- list or compound-list
- compound command
- function definition
.fi
.LP
Unless otherwise stated, the exit status of a command is
that of the last simple command executed by the command.
.sp2
.BPipeline
.sp
.LP
A pipeline is a sequence of one or more commands separated
by the control operator |. The standard output of all but
the last command is connected to the standard input
of the next command.
.LP
The format for a pipeline is:
.nf
[!] command1 [ | command2 ...]
.fi
.LP
The standard output of command1 is connected to the standard
input of command2. The standard input, standard output, or
both of a command is considered to be assigned by the
pipeline before any redirection specified by redirection
operators that are part of the command.
.LP
If the pipeline is not in the background (discussed later),
the shell waits for all commands to complete.
.LP
If the reserved word ! does not precede the pipeline, the
exit status is the exit status of the last command specified
in the pipeline. Otherwise, the exit status is the logical
NOT of the exit status of the last command. That is, if
the last command returns zero, the exit status is 1; if
the last command returns greater than zero, the exit status
is zero.
.LP
Because pipeline assignment of standard input or standard
output or both takes place before redirection, it can be
modified by redirection. For example:
.nf
$ command1 2>&1 | command2
.fi
sends both the standard output and standard error of command1
to the standard input of command2.
.LP
A ; or <newline> terminator causes the preceding
AND-OR-list (described next) to be executed sequentially; a & causes
asynchronous execution of the preceding AND-OR-list.
.sp2
.BBackgroundCommands--&
.sp
.LP
If a command is terminated by the control operator ampersand
(&), the shell executes the command asynchronously -- that is,
the shell does not wait for
the command to finish before executing the next command.
.LP
The format for running a command in background is:
.nf
command1 & [command2 & ...]
.fi
If the shell is not interactive, the standard input of an
asynchronous command is set to /dev/null.
.sp2
.BLists--GenerallySpeaking
.sp
.LP
A list is a sequence of zero or more commands separated by
newlines, semicolons, or ampersands,
and optionally terminated by one of these three characters.
The commands in a
list are executed in the order they are written.
If command is followed by an ampersand, the shell starts the
command and immediately proceed onto the next command;
otherwise it waits for the command to terminate before
proceeding to the next one.
.LP
``&&'' and ``||'' are AND-OR list operators. ``&&'' executes
the first command, and then executes the second command
iff the exit status of the first command is zero. ``||''
is similar, but executes the second command iff the exit
status of the first command is nonzero. ``&&'' and ``||''
both have the same priority.
.LP
The syntax of the if command is
.nf
if list
then list
[ elif list
then list ] ...
[ else list ]
fi
.fi
The syntax of the while command is
.nf
while list
do list
done
.fi
The two lists are executed repeatedly while the exit status of the first list is zero. The until command is similar, but has the word until in place of while
repeat until the exit status of the first list is zero.
.LP
The syntax of the for command is
.nf
for variable in word...
do list
done
.fi
The words are expanded, and then the list is executed
repeatedly with the variable set to each word in turn. do
and done may be replaced with ``{'' and ``}''.
.LP
The syntax of the break and continue command is
.nf
break [ num ]
continue [ num ]
.fi
Break terminates the num innermost for or while loops.
Continue continues with the next iteration of the innermost loop. These are implemented as builtin commands.
.LP
The syntax of the case command is
.nf
case word in
pattern) list ;;
...
esac
.fi
.LP
The pattern can actually be one or more patterns (see Shell
Patterns described later), separated by ``|'' characters.
.LP
Commands may be grouped by writing either
.nf
(list)
.fi
or
.nf
{ list; }
.fi
The first of these executes the commands in a subshell.
.sp2
.BFunctions
.sp
.LP
The syntax of a function definition is
.nf
name ( ) command
.fi
.LP
A function definition is an executable statement; when
executed it installs a function named name and returns an
exit status of zero. The command is normally a list
enclosed between ``{'' and ``}''.
.LP
Variables may be declared to be local to a function by
using a local command. This should appear as the first
statement of a function, and the syntax is
.nf
local [ variable | - ] ...
.fi
Local is implemented as a builtin command.
.LP
When a variable is made local, it inherits the initial
value and exported and readonly flags from the variable
with the same name in the surrounding scope, if there is
one. Otherwise, the variable is initially unset. The shell
uses dynamic scoping, so that if you make the variable x