SH(1SH) COMMAND REFERENCE SH(1SH)
NAME
sh - command language
SYNOPSIS
sh [ -acefhiknrstuvx ] [ arg ] ...
DESCRIPTION
Sh is a command programming language that executes commands
read from a terminal or a file. This document describes the
syntax of the language understood by sh.
CONTROL STATEMENTS:
for name [in word ...] do list done
for name [in word ...] { list }
case word in [pattern [ | pattern ] ... ) list ;;] ... esac
if list then list [elif list then list] ... [else list] fi
while list [do list] done
until list [do list] done
( list )
Execute list in a subshell.
{ list; }
List is simply executed.
name () { list; }
Define a function which is referenced by name. The body
of the function is a list of commands between { and }.
See the section Functions for more information.
. filename
Commands from the named file are read and executed by
the current shell. The search path specified by the
environment variable PATH is used to find the file.
: ...
The null command. All arguments are evaluated. This can
be used as a comment.
# ...
The comment character. Ignore all text until the end of
the line.
command | command
Both commands are executed, with the standard output of
the first command connected to the standard input of the
Printed 10/17/86 1
SH(1SH) COMMAND REFERENCE SH(1SH)
second command. This is called a ``pipe''.
command && command
The first command is executed, and if the exit status
returned is nonzero, the second command is executed.
command || command
The first command is executed, and if the exit status
returned is zero, the second command is executed.
` command `
Is replaced by the output from command. Backquotes may
be nested by escaping the ` character using a backslash
character (\). The number of backslashes required for
each level of nesting is twice that of the previous
level plus one. So, for example, the first level of
nesting requires one backslash, the second requires
three, the third seven, and so on. There is no imposed
limit to the number of nesting levels.
In the above, name refers to a shell variable name (without
the preceding $), word refers to an entity which expands to
list of one or more sequences of characters separated by the
field separator (see the IFS variable), list refers to one
or more commands separated by newlines or semicolons,
pattern refers to a single sequence of characters possibly
containing the special characters *, [, ], and ? (see
FILENAME GENERATION ), and command refers to an executable
file or builtin command name, followed by zero or more
command arguments.
INTERACTIVE SHELLS
When an interactive shell is started and the variable ENV is
set to the name of an existing file, commands are read from
that file and executed. This is usually used to define
functions. For security reasons, this only happens for
interactive shells. There is no default value for ENV, so
this file must be explicitly executed from, if desired, in
the .profile file (described below).
If the name of the shell begins with a dash (-), commands
are read from the files /etc/profile (or /etc/rprofile for
restricted shells) and .profile (note that the .profile file
in the current directory is executed, and that most programs
that start up shells with the intent of executing the user's
.profile should change directory to the user's home
directory). Shells with names beginning with a dash (-) are
executed by login(1) and possibly by su(1).
PARAMETER SUBSTITUTION
The character $ is used to introduce substitutable
Printed 10/17/86 2
SH(1SH) COMMAND REFERENCE SH(1SH)
parameters (also called variables). Variables may be set by
writing
name=value [ name=value ] ...
${parameter}
A parameter is a sequence of letters, digits or
underscores (a name), a digit, or any of the characters
* @ # ? - $ !. The value, if any, of the parameter is
substituted. The braces are required only when
parameter is followed by a letter, digit, or underscore
that is not to be interpreted as part of its name.
Variable names containing only digits (such as $1) are
called positional parameters. These are set to the
arguments given to the current shell script.
Pattern matching is not performed on the value given.
The following are special parameter substitution forms. In
each of the forms, the `:' following the parameter is
optional. If given, the parameter must be both set and
non-null for it to be substituted, as opposed to only having
to be set when the `:' is not given.
${parameter:-word}
If parameter is set, substitute its value; otherwise
substitute word.
${parameter:=word}
If parameter is not set, set it to word; the value of
the parameter is then substituted. Positional
parameters may not be assigned in this way.
${parameter:?word}
If parameter is set, substitute its value; otherwise,
print word and exit from the shell.
${parameter:+word}
If parameter is set, substitute word; otherwise
substitute nothing.
For a description of parameters set and used by the shell,
see the VARIABLES section.
FILENAME GENERATION
If one of these characters appears, the word is regarded as
a pattern. Except when used as the pattern in a case
statement, the word is replaced with alphabetically sorted
filenames that match the pattern. If no filename is found
that matches the pattern, the word is left unchanged. The
dot character ( . ) at the start of a filename or
Printed 10/17/86 3
SH(1SH) COMMAND REFERENCE SH(1SH)
immediately following a slash ( / ) and the slash character
( / ) 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 a dash ( - ) matches any
character lexically between the pair.
The option -f turns off filename generation.
QUOTING
Characters between single quotes are not processed.
Characters between double quotes have macros and commands
substituted, but IFS characters are ignored (see VARIABLES).
"$*" is equivalent to "$1 $2 ..." whereas
"$@" is equivalent to "$1" "$2" ... .
INPUT OUTPUT
<filename
Use filename as standard input.
>filename
Use filename as standard output. If the file does not
exist, it is created; otherwise it is truncated to zero
length.
>>filename
Use filename as standard output. If the file exists,
output is appended; otherwise the file is created.
<<[-]string
The shell input is read up to a line the same as string,
or end-of-file. The resulting document becomes the
standard input. If any character of string is quoted,
no interpretation is placed upon the characters of the
document; otherwise, parameter and command substitution
occurs, \<newline> is ignored, and a backslash ( \ ) is
used to quote the characters \ , $ , ' , and the first
character of string. If a dash (-) is appended to <<,
all leading tabs are stripped from string and from the
document.
<&digit
The standard input is duplicated from file descriptor
digit; Similarly for the standard output using >.
<&- The standard input is closed. Similarly for the
standard output using >.
Printed 10/17/86 4
SH(1SH) COMMAND REFERENCE SH(1SH)
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:
... 2>&1
creates file descriptor 2 to be a duplicate of file
descriptor 1. The standard for file descriptor numbers is
that 0 is standard input, 1 is standard output, and 2 is
standard error.
FUNCTIONS
If a given command name does not match a builtin command but
matches a defined function, the commands associated with the
function are executed in the current shell (similar to the
alias mechanism in csh(1csh)). The positional parameters $1,
$2, ... are set to the arguments of the function. A
function is executed just like any other program with one
major exception: if the function is executed in the current
shell, its arguments replace the current positional
parameters, so the original parameter values are lost. This
can be prevented by running the function in a subshell.
Note that functions with names the same as builtin commands
are ignored.
The command lsh(1sh) is a limited version of sh. In some
systems, this is called rsh, but is called lsh here because
of the remote shell command, rsh.
OPTIONS
-a Mark all variables which are modified or created for
export (see export(1sh)).
-c string
If the -c flag is present, commands are read from
string.
-e If noninteractive, exit immediately if a command fails.
-f Disable filename generation.
-h Hash commands used in functions when functions are
defined.
-i If the -i flag is present or if the shell input and
output are attached to a terminal (as told by gtty) then
this shell is interactive. In this case the terminate
signal SIGTERM (see signal(3c)) is ignored (so that the
input kill 0 does not kill an interactive shell) and the
interrupt signal SIGINT is caught and ignored (so that
Printed 10/17/86 5
SH(1SH) COMMAND REFERENCE SH(1SH)
wait is interruptible).
-k All keyword arguments are placed in the environment for
a command, not just those that precede the command name.
-n Read commands but do not execute them.
-s If the -s flag is present or if no arguments remain,
then commands are read from the standard input. Shell
output is written to file descriptor 2.
-t Exit after reading and executing one command.
-u Treat unset variables as an error when substituting.
-v Print shell input lines as they are read.
-x Print commands and their arguments as they are executed.
FILES
$HOME/.profile This file is read and commands
contained in it executed when the
shell is called as -sh, usually at
login.
/tmp/sh* Temporary storage for storing
arguments to <<.
DIAGNOSTICS
Errors detected by the shell, such as syntax errors cause
the shell to return a nonzero 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 exit).
VARIABLES
These variables are set by the shell:
$0 The name of the current shell or script.
$1, $2, ... Arguments given to the current shell or
script.
$* $1, $2, ... collectively.
$@ Same as $*, except when quoted.
$# The number of positional parameters.
$- Options currently set.
$? The exit status of the last command executed.
Printed 10/17/86 6
SH(1SH) COMMAND REFERENCE SH(1SH)
$$ The process ID of this shell.
$! The process ID of the last background command
invoked.
These variables are used but not set by the shell.
$CDPATH The search path for the cd command.
$HOME The user's home directory.
$PATH The search path for command execution.
$MAIL The name of the file to be searched for
notification of new mail.
$MAILCHECK The number of seconds between checks for new
mail (default is 600).
$MAILPATH Colon-separated list of files to check for
mail new mail.
$PS1 Primary prompt string (default $ ).
$PS2 Secondary prompt string (default > ).
$IFS Field separator (default tab, space, and
newline).
$SHELL The name of the shell. If the name has an r
in it, the shell is restricted.
$ENV The name of the startup file for all
interactive shells. Used for defining
functions.
CAVEATS
Builtin commands such as set and read may not have their
input or output redirected. In order to redirect a command
like set, it must be executed in a subshell or by using
exec. For example, the command
(set) > set.out
will print the values of exported names. Note that this does
not produce the same output as would set in the current
shell, since not all variables are exported.
If << 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
Printed 10/17/86 7
SH(1SH) COMMAND REFERENCE SH(1SH)
the shell complains about not being able to find the file by
another name.
Signal 11 (segmentation violation) can not be trapped.
The execution path is hashed for faster execution. See the
manual page for hash(1sh) for more information.
The first line of all shell scripts should be of the form
#!/bin/sh or #!/bin/csh (see execve(2) for more
information). In some systems, executable files whose first
line is not of the form described above and whose first line
begins with #, are executed by csh(1csh). This is not true
in this system.
SEE ALSO
break(1sh), cd(1sh), chdir(1sh), continue(1sh), echo(1sh),
eval(1sh), exec(1sh), exit(1sh), export(1sh), hash(1sh),
login(1), lsh(1sh), pwd(1sh), read(1sh), readonly(1sh),
return(1sh), set(1sh), shift(1sh), test(1sh), times(1sh),
trap(1sh), type(1sh), ulimit(1sh), umask(1sh), unset(1sh),
wait(1sh), which(1sh), execve(2).
Printed 10/17/86 8
%%index%%
na:72,57;
sy:129,206;
de:335,2463;2942,3155;6241,2720;9105,2608;11857,1600;
op:13457,1039;14640,749;
fi:15389,412;
di:15801,414;
va:16215,527;16886,1174;
ca:18060,829;19033,668;
se:19701,856;
%%index%%000000000225