sh - Shell, the standard command language interpreter
(POSIX Shell)
sh [-ir] [-c command_string | -s] [+ | -abCefhkmnopstuvx]
[+|-o] [option...]|[+|-A name] [argument...]|[file] [argument...]
The POSIX shell is an interactive command interpreter and
a command programming language.
Interfaces documented on this reference page conform to
industry standards as follows:
sh: XCU5.0 and POSIX.2
Refer to the standards(5) reference page for more information
about industry standards and associated tags.
Causes sh to read commands from command_string. Causes sh
to run as an interactive shell. The SIGTERM signal is
thus ignored, and the SIGINT signal is caught, causing the
current command to be terminated and a new prompt to be
output. [Tru64 UNIX] Causes sh to run as a restricted
shell. Causes sh to read commands from standard input. If
you do not specify the -c option or do not specify any
arguments to sh other than options, sh automatically
invokes the -s option. The -c option overrides the -s
option.
[Tru64 UNIX] The rest of the options that can be used
with sh are described under the set subcommand in the subsection
Special sh Commands.
The POSIX shell carries out commands either interactively
from a terminal keyboard or from a file.
Some important features of the shell are as follows: Command
aliasing File name substitution Tilde substitution
Command substitution Parameter substitution Job control
Inline editing
A file from which the shell carries out commands is usually
called a shell script, a shell procedure, or a command
file.
A simple command is a sequence of words separated by
spaces or tabs. A word is a sequence of characters that
contains no unquoted spaces or tabs. The first word in
the sequence (numbered as 0), usually specifies the name
of a command. Any remaining words, with a few exceptions,
are passed to that command. A space refers to both spaces
and tabs.
[Tru64 UNIX] The value of a simple command is its exit
value if it ends normally, or (octal) 200 added to the
signal number if it terminates due to a signal. For a
list of status values, see the signal(2) reference page.
A pipeline is a sequence of one or more commands separated
by a | (vertical bar) or, for historical compatibility, by
a ^ (circumflex). In a pipeline, the standard output of
each command becomes the standard input of the next command.
Each command runs as a separate process, and the
shell waits for the last command to end. A filter is a
command that reads its standard input, transforms it in
some way, then writes it to its standard output. A
pipeline normally consists of a series of filters.
Although the processes in a pipeline (except the first
process) can execute in parallel, they are synchronized to
the extent that each program needs to read the output of
its predecessor.
The exit value of a pipeline is the exit value of the last
command.
A list is a sequence of one or more pipelines separated by
; (semicolon), & (ampersand), && (two ampersands), or ||
(two vertical bars) and optionally ended by a ; (semicolon),
an & (ampersand), a |& (coprocess), or a newline.
These separators and terminators have the following
effects: Causes sequential execution of the preceding
pipeline; the shell waits for the pipeline to finish.
Causes asynchronous execution of the preceding pipeline;
the shell does not wait for the pipeline to finish.
Causes the list following it to be executed only if the
preceding pipeline returns a 0 (zero) exit value. Causes
the list following it to be executed only if the preceding
pipeline returns a nonzero exit value.
[Tru64 UNIX] The cd command is an exception; if it
returns a nonzero exit value, no subsequent commands
in a list are executed, regardless of the
separator characters.
The ; and & separators have equal precedence, as do && and
||. The single-character separators have lower precedence
than the double-character separators. An unquoted newline
character following a pipeline functions the same as a ;
(semicolon).
Comments [Toc] [Back]
The shell treats as a comment any word that begins with a
# character and ignores that word and all characters following
up to the next newline character.
Shell Flow Control Statements [Toc] [Back]
Unless otherwise stated, the value returned by a command
is that of the last simple command executed in the command.
Each time a for command is executed, identifier is
set to the next word taken from the in word list. If in
word ... is omitted, the for command executes the do list
once for each positional parameter that is set. (See
Parameter Substitution.) Execution ends when there are no
more words in the list. Prints on standard error (file
descriptor 2), the set of words, each preceded by a number.
If in word... is omitted, then the positional
parameters are used instead. (See Parameter Substitution.)
The PS3 prompt is printed and a line is read from
the standard input. If this line consists of the number
of one of the listed words, then the value of the parameter
identifier is set to the word corresponding to this
number. If this line is empty, the selection list is
printed again. Otherwise, the value of the parameter
identifier is set to null. The contents of the line read
from standard input is saved in the REPLY parameter. The
list is executed for each selection until a break or Endof-File
is encountered. Executes the list associated with
the first pattern that matches word. The form of the patterns
is the same as that used for file name generation.
(See File name Generation.) Executes the list following
if and, if it returns a 0 (zero) exit status, executes the
list following the first then. Otherwise, the list following
elif is executed and, if its value is 0 (zero), the
list following the next then is executed. Failing that,
the else list is executed. If no else list or then list
is executed, then the if command returns a 0 (zero) exit
status. Executes the while list repeatedly, and if the
exit status of the last command in the list is 0 (zero),
executes the do list; otherwise the loop terminates. If
no commands in the do list are executed, then the while
command returns a 0 (zero) exit status; until can be used
in place of while to negate the loop termination test.
Executes list in a separate environment. If two adjacent
open parentheses are needed for nesting, a space must be
inserted to avoid arithmetic evaluation as described
later. Executes list. Unlike the metacharacters ( and ),
{ and } are reserved words and must be at the beginning of
a line or after a ; (semicolon) in order to be recognized.
Evaluates expression and returns a 0 (zero) exit status
when expression is TRUE. See Conditional Expressions for
a description of expression. Defines a function that is
referenced by identifier. The body of the function is the
list of commands between { and }. (See Functions.) Executes
pipeline and prints the elapsed time as well as the
user and system time on standard error.
The following reserved words are recognized only when they
appear, without single or double quotes, as the first word
of a command:
if for case then while esac else until function
elif do select fi done time { } [[ ]]
Command Aliasing [Toc] [Back]
The first word of each command is replaced by the text of
an alias (if an alias for this word was defined). The
first character of an alias name can be any nonspecial
printable character, but the rest of the characters must
be the same as for a valid identifier. The replacement
string can contain any valid shell script, including the
metacharacters previously listed. The first word of each
command in the replaced text, other than any that are in
the process of being replaced, is tested for aliases. If
the last character of the alias value is a space, the word
following the alias will also be checked for alias substitution.
Aliases can be used to redefine special built-in commands
but cannot be used to redefine the reserved words previously
listed. Aliases can be created, listed, and
exported with the alias command and can be removed with
the unalias command. Exported aliases remain in effect
for scripts invoked by name, but must be reinitialized for
separate invocations of the shell. (See Invocation.)
Aliasing is performed when scripts are read, not while
they are executed. Therefore, for an alias to take
effect, the alias definition command has to be executed
before the command that references the alias is read.
Aliases are frequently used as shorthand for full pathnames.
An option to the aliasing facility allows the
value of the alias to be automatically set to the full
pathname of the corresponding command. These aliases are
called tracked aliases.
[Tru64 UNIX] The value of a tracked alias is defined the
first time the corresponding command is looked up and
becomes undefined each time the PATH environment variable
is reset. These aliases remain tracked so that the next
subsequent reference will redefine the value. Several
tracked aliases are compiled into the shell. The -h
option of the set command makes each referenced command
name into a tracked alias.
[Tru64 UNIX] The following exported aliases are compiled
into the shell, but can be unset or redefined:
autoload='typeset -fu' false='let 0' functions='typeset
-f' hash='alias -t' history='fc -l' integer='typeset -i'
nohup='nohup ' r='fc -e -' true=':' type='whence -v'
Tilde Substitution [Toc] [Back]
After alias substitution is performed, each word is
checked to see if it begins with an unquoted ~ (tilde).
If it does, then the word up to a / (slash) is checked to
see if it matches a username in the /etc/passwd file. If
a match is found, the tilde and the matched login name are
replaced by the login directory of the matched user. This
is called a tilde substitution. If no match is found, the
original text is left unchanged. A tilde by itself, or in
front of a /, is replaced by the value of the HOME parameter.
A tilde followed by a + (plus sign) or - (dash) is
replaced by $PWD and $OLDPWD, respectively.
In addition, tilde substitution is attempted when the
value of a variable assignment parameter begins with a
tilde.
Command Substitution [Toc] [Back]
The standard output from a command enclosed in parentheses
preceded by a dollar sign $( ) or a pair of `` (grave
accents) can be used as part or all of a word; trailing
newlines are removed. In the second (archaic) form, the
string between the grave accents is processed for special
quoting characters before the command is executed. (See
Quoting.) The command substitution $(cat file) can be
replaced by the equivalent but faster $(<file). Command
substitution of most special commands that do not perform
input/output redirection are carried out without creating
a separate process. An arithmetic expression enclosed in
double parentheses preceded by a dollar sign ( $(( )) ) is
replaced by the value of the arithmetic expression within
the double parentheses.
Parameter Substitution [Toc] [Back]
A parameter is an identifier, one or more digits, or any
of the characters *, @, #, ?, -, $, and !. A named parameter
(a parameter denoted by an identifier) has a value
and 0 (zero) or more attributes. Named parameters can be
assigned values and attributes by using the typeset special
command. The attributes supported by the shell are
described later with the typeset special command.
Exported parameters pass values and attributes to the
environment.
The shell supports a 1-dimensional array facility. An
element of an array parameter is referenced by a subscript.
A subscript is denoted by an arithmetic expression
enclosed with [ ] (brackets). To assign values to an
array, use set -A name value ... The value of all subscripts
must be in the range of 0 to 1023. Arrays need
not be declared. Any reference to a named parameter with
a valid subscript is legal and an array is created if necessary.
Referencing an array without a subscript is
equivalent to referencing the element 0 (zero).
The value of a named parameter can be assigned by the following:
name=value [ name=value ]
If the integer attribute, -i, is set for name, the value
is subject to arithmetic evaluation, as described later.
Positional parameters, which are denoted by a number, can
be assigned values with the set special command. Parameter
$0 is set from argument 0 (zero) when the shell is
invoked. The $ (dollar sign) character is used to introduce
substitutable parameters. Reads all the characters
from the ${ (dollar sign left brace) to the matching }
(right brace) as part of the same word even if it contains
braces or metacharacters. The value, if any, of the
parameter is substituted. The braces are required when
parameter is followed by a letter, digit, or underscore
that is not to be interpreted as part of its name or when
a named parameter is subscripted. If parameter is one or
more digits, it is a positional parameter. A positional
parameter of more than one digit must be enclosed in
braces. If parameter is * (asterisk) or @ (at sign), all
the positional parameters, starting with $1, are substituted
(separated by a field separator character). If an
array identifier with subscript * or @ is used, the value
for each of the elements is substituted (separated by a
field separator character). Substitutes the number of
positional parameters if parameter is * or @; otherwise,
the length of the value of the parameter is substituted.
Substitutes the number of elements in the array identifier.
Substitutes the value of parameter if it is set and
non-null; otherwise, substitute word. Sets parameter to
word if it is not set or is null; the value of the parameter
is then substituted. Positional parameters cannot be
assigned values in this way. Substitutes the value of
parameter if it is set and is non-null; otherwise, print
word and exit from the shell. If word is omitted, a standard
message is printed. Substitute word if parameter is
set and is non-null; otherwise, substitute nothing.
Causes the value of this substitution to be the value of
parameter with the matched portion deleted if the shell
pattern matches the beginning of the value of parameter;
otherwise the value of parameter is substituted. In the
first form, the smallest matching pattern is deleted and
in the second form, the largest matching pattern is
deleted. Causes the value of this substitution to be the
value of parameter with the matched part deleted if the
shell pattern matches the end of the value of parameter;
otherwise, substitute the value of parameter. In the
first form, the smallest matching pattern is deleted and
in the second form, the largest matching pattern is
deleted.
If the : (colon) is omitted from the previous expressions,
then the shell checks only whether parameter is set or
not.
In the previous expressions, word is not evaluated unless
it is to be used as the substituted string, so that, in
the following example, pwd is executed only if d is not
set or is null: echo ${d:-$(pwd)}
The following parameters are automatically set by the
shell: The number of positional parameters in decimal.
Flags supplied to the shell on invocation or by the set
command. The decimal value returned by the last executed
command. The process number of this shell. [Tru64
UNIX] Initially, the value of _ (underscore) is an absolute
pathname of the shell or script being executed as
passed in the environment. Subsequently, it is assigned
the last argument of the previous command. This parameter
is not set for commands that are asynchronous. This
parameter is also used to hold the name of the matching
MAIL file when checking for mail. The process number of
the last background command invoked. [Tru64 UNIX] The
value of errno as set by the most recently failed system
call. This value is system dependent and is intended for
debugging purposes. The line number of the current line
within the script or function being executed. The previous
working directory set by the cd command. The value of
the last option argument processed by the getopts special
command. The index of the last option argument processed
by the getopts special command. The process number of the
parent of the shell. The present working directory set by
the cd command. [Tru64 UNIX] Each time this parameter is
referenced, a random integer, uniformly distributed
between 0 and 32767, is generated. The sequence of random
numbers can be initialized by assigning a numeric value to
RANDOM. [Tru64 UNIX] This parameter is set by the select
statement and by the read special command when no arguments
are supplied. [Tru64 UNIX] Each time this parameter
is referenced, the number of seconds since shell invocation
is returned. If this parameter is assigned a
value, then the value returned upon reference is the value
that was assigned plus the number of seconds since the
assignment.
The following parameters are used by the shell: The search
path for the cd command. If this variable is set, the
value is used to define the width of the edit window for
the shell edit modes and for printing select lists. If
the value of this variable ends in emacs, gmacs, or vi and
the VISUAL variable is not set, then the corresponding
option (see set under Special sh Commands) is turned on.
If this parameter is set, then parameter substitution is
performed on the value to generate the pathname of the
script that is executed when the shell is invoked. (See
Invocation.) This file is typically used for alias and
function definitions. The default editor name for the fc
command. [Tru64 UNIX] The search path for function definitions.
This path is searched when a function with the
-u attribute is referenced and when a command is not
found. If an executable file is found, then it is read
and executed in the current environment. Internal field
separators, normally spaces, tabs, and newlines that are
used to separate command words which result from command
or parameter substitution and for separating words with
the read special command. The first character of the IFS
parameter is used to separate arguments for the $* substitution.
(See Quoting.) If this parameter is set when the
shell is invoked, then the value is the pathname of the
file that is used to store the command history. (See Command
Reentry.) If this parameter is set when the shell is
invoked, the number of previously entered commands that
are accessible by this shell is greater than or equal to
this number. The default is 128. The default argument
(home directory) for the cd command. Specifies the locale
of your system, which is comprised of three parts: language,
territory, and codeset. The default locale is the
C locale, which specifies the value English for language,
U.S. for territory, and ASCII for codeset. The locale
specified for the LANG variable controls the language
applied to messages. Unless set explicitly, the LC_COLLATE,
LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, and
LC_TIME variables also derive their settings from the
locale set for LANG. Specifies the collating sequence to
use when sorting names and when character ranges occur in
patterns. The default value is the collating sequence for
American English. If absent, the collating sequence can
be taken from the LANG parameter. If both LC_COLLATE and
LANG are absent, the ANSI C collating sequence is used.
Specifies the character classification information to use
on your system. The default value is American English.
Specifies the language that the system expects for user
input of yes and no strings. The default value is American
English. Specifies the monetary format for your system.
The default value is the monetary format for American
English. Specifies the numeric format for your system.
The default value is the numeric format for American
English. Specifies the date and time format for your system.
The default value is the date and time format for
American English. [Tru64 UNIX] If this variable is set,
the value is used to determine the column length for
printing select lists. Select lists will print vertically
until about two-thirds of LINES lines are filled. [Tru64
UNIX] The name of the user's login account corresponding
to the login name in the user database. If this parameter
is set to the name of a mail file and the MAILPATH parameter
is not set, the shell informs you of the arrival of
mail in the specified file. This variable specifies how
often (in seconds) the shell checks for changes in the
modification time of any of the files specified by the
MAILPATH or MAIL parameters. The default value is 600
seconds. When the time has elapsed, the shell checks
before issuing the next prompt. A list of file names separated
by : (colons). If this parameter is set, the shell
informs you of any modifications to the specified files
that have occurred within the last MAILCHECK seconds.
Each file name can be followed by a ? (question mark) and
a message that is printed. The message will undergo
parameter substitution with the parameter, $_ defined as
the name of the file that has changed. The default message
is you have mail in $_. Specifies a list of directories
to search to find message catalogs. The search path
for commands. (See Execution.) The value of this parameter
is expanded for parameter substitution to define the
primary prompt string which by default is the $ (dollar
sign). The ! (exclamation point) in the primary prompt
string is replaced by the command number. (See Command
Reentry.) Secondary prompt string, by default > (right
angle bracket). Selection prompt string used within a
select loop, by default #? (number sign, question mark).
The value of this parameter is expanded for parameter substitution
and precedes each line of an execution trace.
If omitted, the execution trace prompt is + (plus sign).
[Tru64 UNIX] The pathname of the shell is kept in the
environment. [Tru64 UNIX] If set to a value greater than
0 (zero), the shell terminates if a command is not entered
within the prescribed number of seconds after issuing the
PS1 prompt. (Note that the shell can be compiled with a
maximum bound for this value that cannot be exceeded.) If
the value of this variable ends in emacs, gmacs, or vi,
the corresponding option (see the set command in Special
sh Commands) is turned on.
[Tru64 UNIX] The shell gives default values to PATH, PS1,
PS2, MAILCHECK, TMOUT, and IFS, while HOME, SHELL, ENV,
and MAIL are not set by the shell (although HOME is set by
the login command). On some systems, MAIL and SHELL are
also set by the login command.
Interpretation of Spaces [Toc] [Back]
After parameter and command substitution, the results of
substitutions are scanned for the field separator characters
(those found in IFS), and split into distinct arguments
where such characters are found. Explicit null
arguments (`` or '') are retained. Implicit null arguments
(those resulting from parameters that have no values)
are removed.
File name Generation [Toc] [Back]
Following substitution, each command word is scanned for
the characters * (asterisk), ? (question mark), and [ ]
(brackets), unless the -f option was set. If one of these
characters appears, the word is regarded as a pattern.
The word is replaced with lexicographically sorted file
names that match the pattern. If no file name is found
that matches the pattern, the word is left unchanged.
When a pattern is used for file name generation, the (dot)
character at the start of a file name or immediately following
a / (slash), as well as the / character itself,
must be matched explicitly. In other instances of pattern
matching, the / and are not treated specially. Matches
any string, including the null string. Matches any single
character. Matches any one of the enclosed characters.
In an expression such as [a-z], the - (dash) means
"through" according to the current collating sequence.
The collating sequence is determined by the value of the
LC_COLLATE environment variable. If the first character
following the [ (left bracket) is a ! (exclamation
point), then any character not enclosed is matched. A -
can be included in the character set by putting it as the
first or last character.
A pattern_list is a list of one or more patterns separated
from each other with a | (vertical bar). Composite patterns
can be formed with one or more of the following:
Optionally matches any one of the given patterns. Matches
zero or more occurrences of the given patterns. Matches
one or more occurrences of the given patterns. Matches
exactly one of the given patterns. Matches anything,
except one of the given patterns.
Character Classes [Toc] [Back]
You can use the following notation to match file names
within a range indication:
[:charclass:]
This format instructs the system to match any single character
belonging to charclass; the defined classes correspond
to ctype() subroutines as follows:
alnum alpha cntrl digit graph lower print punct space
upper xdigit
Your locale might define additional character properties,
such as the following:
[:vowel:]
The preceding character class could be TRUE for a, e, i,
o, u, or y. You could then use [:vowel] inside a set construction
to match any vowel. Refer to The LC_CTYPE Category
section of the locale file format reference page for
more information.
Quoting [Toc] [Back]
The following characters have a special meaning to the
shell and cause termination of a word unless quoted:
; & ( ) | ^ < > <newline> <space> <tab>
Each of the metacharacters previously listed has a special
meaning to the shell and causes termination of a word
unless quoted. A character can be quoted (that is, made
to stand for itself) by preceding it with a \ (backslash).
The pair \newline is ignored. All characters enclosed
between a pair of '' (single quotes) are quoted. A single
quote cannot appear within single quotes.
Inside "" (double quotes) parameter and command substitution
occurs and \ quotes the characters \, `, ', and $.
The meaning of $* and $@ is identical when not quoted or
when used as a parameter assignment value or as a file
name. However, when used as a command argument, '$*' is
equivalent to '$1d$2d. . .', where d is the first character
of the IFS parameter, whereas '$@' is equivalent to
'$1' '$2' . . . Inside `` (grave accents) \ (backslash)
quotes the characters \, `, and $. If the grave accents
occur within double quotes, then \ also quotes the ' (single
quote) character.
The special meaning of reserved words or aliases can be
removed by quoting any character of the reserved word.
The recognition of function names or special command names
listed later cannot be altered by quoting them.
Arithmetic Evaluation [Toc] [Back]
[Tru64 UNIX] An ability to perform integer arithmetic is
provided with the let special command. Evaluations are
performed using long arithmetic. Constants are of the
form [base#]n, where base is a decimal number between 2
and 36 representing the arithmetic base and n is a number
in that base. If base is omitted, then base 10 is used.
[Tru64 UNIX] An arithmetic expression uses the same syntax,
precedence, and associativity of expression as the C
language. All the integral operators, other than ++, --,
?:, and , are supported. Named parameters can be referenced
by name within an arithmetic expression without
using the parameter substitution syntax. When a named
parameter is referenced, its value is evaluated as an
arithmetic expression.
An internal integer representation of a named parameter
can be specified with the -i option of the typeset special
command. Arithmetic evaluation is performed on the value
of each assignment to a named parameter with the -i
attribute. If you do not specify an arithmetic base, the
first assignment to the parameter determines the arithmetic
base. This base is used when parameter substitution
occurs.
Because many of the arithmetic operators require quoting,
an alternative form of the let command is provided. For
any command that begins with a ((, all the characters
until a matching )) are treated as a quoted expression.
More precisely, ((...)) is equivalent to let "...".
Note that ((...)) is a command with a return value,
whereas $((...)) is the way to put the string representation
of the value of an arithmetic expression into the
command line (that is, it is like a $ variable).
Prompting [Toc] [Back]
When used interactively, the shell prompts with the value
of PS1 before reading a command. If at any time a newline
is typed and further input is needed to complete a command,
then the secondary prompt (that is, the value of
PS2) is issued.
Conditional Expressions [Toc] [Back]
A conditional expression is used with the [[ compound command
to test attributes of files and to compare strings.
Word splitting and file name generation are not performed
on the words between [[ and ]]. Each expression can be
constructed from one or more of the following unary or
binary expressions: [Tru64 UNIX] TRUE, if file exists.
[Tru64 UNIX] TRUE, if file exists and is a block-special
file. [Tru64 UNIX] TRUE, if file exists and is a character-special
file. [Tru64 UNIX] TRUE, if file exists and
is a directory. [Tru64 UNIX] TRUE, if file exists.
[Tru64 UNIX] TRUE, if file exists and is an ordinary
file. [Tru64 UNIX] TRUE, if file exists and has its setgid
bit set. [Tru64 UNIX] TRUE, if file exists and its
group matches the effective group ID of this process.
[Tru64 UNIX] TRUE, if file exists and has its sticky bit
set. [Tru64 UNIX] TRUE, if file exists and is a symbolic
link. [Tru64 UNIX] TRUE, if length of string is nonzero.
[Tru64 UNIX] TRUE, if option named option is on. [Tru64
UNIX] TRUE, if file exists and is owned by the effective
user ID of this process. [Tru64 UNIX] TRUE, if file
exists and is a FIFO special file or a pipe. [Tru64
UNIX] TRUE, if file exists and is readable by current
process. [Tru64 UNIX] TRUE, if file exists and has size
greater than 0 (zero). [Tru64 UNIX] TRUE, if file exists
and is a socket. [Tru64 UNIX] TRUE, if file descriptor
number file_des is open and associated with a terminal
device. [Tru64 UNIX] TRUE, if file exists and has its
setuid bit set. [Tru64 UNIX] TRUE, if file exists and is
writable by current process. [Tru64 UNIX] TRUE, if file
exists and is executable by current process. If file
exists and is a directory, then the current process has
permission to search in the directory. [Tru64
UNIX] TRUE, if length of string is 0 (zero). [Tru64
UNIX] TRUE, if file1 exists and is newer than file2.
[Tru64 UNIX] TRUE, if file1 exists and is older than
file2. [Tru64 UNIX] TRUE, if file1 and file2 exist and
refer to the same file. [Tru64 UNIX] TRUE, if string
matches pattern. [Tru64 UNIX] TRUE, if string does not
match pattern. [Tru64 UNIX] TRUE, if string1 collates
before string2. [Tru64 UNIX] TRUE, if string1 collates
after string2. [Tru64 UNIX] TRUE, if expression1 is
equal to expression2. [Tru64 UNIX] TRUE, if expression1
is not equal to expression2. [Tru64 UNIX] TRUE, if
expression1 is less than expression2. [Tru64 UNIX] TRUE,
if expression1 is greater than expression2. [Tru64
UNIX] TRUE, if expression1 is less than or equal to
expression2. [Tru64 UNIX] TRUE, if expression1 is
greater than or equal to expression2.
[Tru64 UNIX] A compound expression can be constructed
from these primitives by using any of the following,
listed in decreasing order of precedence. [Tru64
UNIX] TRUE, if expression is TRUE. Used to group expressions.
[Tru64 UNIX] TRUE if expression is FALSE. [Tru64
UNIX] TRUE, if expression1 and expression2 are both TRUE.
[Tru64 UNIX] TRUE, if either expression1 or expression2
is TRUE.
Input/Output
Before a command is executed, you can redirect its input
and output by using a special notation interpreted by the
shell. The following can appear anywhere in a simple command
or can precede or follow a command and are not passed
on to the invoked command. Command and parameter substitution
occurs before word or digit is used, except as noted
in the following text. File name generation occurs only
if the pattern matches a single file and interpretation of
spaces is not performed. Use file word as standard input
(file descriptor 0). Use file word as standard output
(file descriptor 1). If the file does not exist, it is
created. If the file exists, and the noclobber option is
on, this causes an error; otherwise, it is truncated to 0
(zero) length. Same as >, except that it overrides the
noclobber option. Use file word as standard output. If
the file exists, output is appended to it (by first seeking
to the End-of-File); otherwise, the file is created.
Open file word for reading and writing as standard input.
The shell input is read up to a line that is the same as
word, or to an End-of-File. No parameter substitution,
command substitution, or file name generation is performed
on word. The resulting document, called a here document,
becomes the standard input. If any character of word is
quoted, then no interpretation is placed upon the characters
of the document; otherwise, parameter and command
substitution occurs, \newline is ignored, and \ must be
used to quote the characters \, $, `, and the first character
of word. If - is appended to <<, then all leading
tabs are stripped from word and from the document. The
standard input is duplicated from file descriptor digit
(see the dup(2) reference page). The standard output is
duplicated using >& digit. The standard input is closed.
The standard output is closed using >&-. The input from
the coprocess (or background process) is moved to standard
input. The output to the coprocess is moved to standard
output.
If one of the preceding redirections is preceded by a
digit, then the file descriptor number referred to is that
specified by the digit (instead of the default 0 or 1).
For example:
... 2>&1
means file descriptor 2 is to be opened for writing as a
duplicate of file descriptor 1.
The order in which redirections are specified is significant.
The shell evaluates each redirection in terms of the
(file descriptor, file) association at the time of evaluation.
For example:
... 1>fname 2>&1
first associates file descriptor 1 with file fname. It
then associates file descriptor 2 with the file associated
with file descriptor 1 (that is, fname). If the order of
redirections were reversed, file descriptor 2 is associated
with the terminal (assuming file descriptor 1 is) and
then file descriptor 1 is associated with file fname.
If a command is followed by & and job control is not
active, the default standard input for the command is the
empty /dev/null file. Otherwise, the environment for the
execution of a command contains the file descriptors of
the invoking shell as modified by input/output specifications.
Environment [Toc] [Back]
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. The names must be identifiers and the values
are character strings. The shell interacts with the
environment in several ways. On invocation, the shell
scans the environment and creates a parameter for each
name found, giving it the corresponding value and marking
it export. Executed commands inherit the environment. If
you modify the values of these parameters or create new
ones, using the export or typeset -x commands, they become
part of the environment. The environment seen by any executed
command is thus composed of any name-value pairs
originally inherited by the shell, whose values can be
modified by the current shell, plus any additions that
must be noted in the export or typeset -x commands.
[Tru64 UNIX] When the value of an exported parameter is
changed, the shell automatically exports the new value to
all child processes. This behavior is different from that
of the Bourne shell, sh(1b), which does not automatically
reexport a changed parameter.
You can augment the environment for any simple command or
function by prefixing it with one or more parameter
assignments. A parameter assignment argument is a word of
the form identifier=value.
Thus, the following two expressions are equivalent (as far
as the execution of command is concerned):
TERM=450 command argument ...
(export TERM; TERM=450; command argument ...)
[Tru64 UNIX] If the -k option is set, all parameter
assignment arguments are placed in the environment, even
if they occur after the command name. The following first
prints a=b c and then c: echo a=b c set -k echo a=b c
[Tru64 UNIX] This feature is intended for use with
scripts written for early versions of the shell; its use
in new scripts is strongly discouraged. It is likely to
disappear someday.
Functions [Toc] [Back]
The function reserved word is used to define shell functions.
Shell functions are read in and stored internally.
Alias names are resolved when the function is read. Functions
are executed like commands with the arguments passed
as positional parameters. (See Execution.)
Functions execute in the same process as the caller and
share all files and the present working directory with the
caller. Traps caught by the caller are reset to their
default action inside the function. A trap condition that
is not caught or ignored by the function causes the function
to terminate and the condition to be passed on to the
caller. A trap on EXIT set inside a function is executed
after the function completes in the environment of the
caller. Ordinarily, variables are shared between the
calling program and the function. However, the special
command typeset used within a function defines local variables
whose scope includes the current function and all
functions it calls.
The special command return is used to return from function
calls. Errors within functions return control to the
caller.
Function identifiers can be listed with the -f or +f
option of the typeset special command. The text of functions
is also listed with -f. Function can be undefined
with the -f option of the unset special command.
Ordinarily, functions are unset when the shell executes a
shell script. The -xf option of the typeset command
allows a function to be exported to scripts that are executed
without a separate invocation of the shell. Functions
that need to be defined across separate invocations
of the shell should be specified in the ENV file with the
-xf option of typeset.
Jobs [Toc] [Back]
If the monitor option of the set command is turned on, an
interactive shell associates a job with each pipeline. It
keeps a table of current jobs, printed by the jobs command,
and assigns them small integer numbers. When a job
is started asynchronously with &, the shell prints a line
that looks like: [1] 1234
This line indicates that the job, which was started asynchronously,
was job number 1 and had one (top-level) process,
whose process ID was 1234.
If you are running a job and want to do something else,
you can enter the Suspend key sequence (normally <Ctrl-z>,
which sends a SIGINT signal to the current job. The shell
then normally indicates that the job has been stopped, and
it prints another prompt. You can then manipulate the
state of this job, putting it in the background with the
bg command, or run some other commands and then eventually
bring the job back into the foreground with the foreground
command fg. The job suspension takes effect immediately,
and corresponds to the Interrupt key sequence in that
pending output and unread input are discarded. A special
key sequence, <Ctrl-y>, does not generate a SIGINT signal
until a program attempts to read it. (See the read(2)
reference page for more information.) This key sequence
can be typed ahead when you have prepared some commands
for a job that you wish to stop after it has read them.
A job being run in the background will stop if it tries to
read from the terminal. Background jobs are normally
allowed to produce output, but this can be disabled by
issuing the stty tostop command. If you set this terminal
option, then background jobs will stop when they try to
produce output like they do when they try to read input.
There are several ways to refer to jobs in the shell. A
job can be referred to by the process ID of any process of
the job, or by one of the following: The job with the
given number. Any job whose command line begins with
string. Any job whose command line contains string. Current
job. Equivalent to %%. Previous job.
This shell learns immediately whenever a process changes
state. It normally informs you whenever a job becomes
blocked so that no further progress is possible, but only
just before it prints a prompt. This is done so that it
does not otherwise disturb your work.
When the monitor mode is on, each background job that is
completed triggers any trap set for CHLD.
When you try to leave the shell while jobs are stopped or
running, you are warned that You have stopped(running)
jobs. You can use the jobs command to see what they are.
If you do this or immediately try to exit again, the shell
does not warn you a second time, and the stopped jobs are
terminated.
Signals [Toc] [Back]
The SIGINT and SIGQUIT signals for an invoked command are
ignored if the command is followed by & and job monitor
option is not active. Otherwise, signals have the values
inherited by the shell from its parent (but see also the
trap command).
Execution [Toc] [Back]
Each time a command is executed, the previous substitutions
are carried out. If the command name matches one of
the special commands listed later, it is executed within
the current shell process. Next, the command name is
checked to see if it matches one of the user-defined functions.
If it does, the positional parameters are saved
and then reset to the arguments of the function call.
When the function is completed or issues a return, the
positional parameter list is restored and any trap set on
EXIT within the function is executed. The value of a
function is the value of the last command executed. A
function is also executed in the current shell process.
If a command name is not a special command or a userdefined
function, a process is created and an attempt is
made to execute the command via exec.
The PATH shell parameter defines the search path for the
directory containing the command. Alternative directory
names are separated by a : (colon). The default path is
:/usr/bin: (specifying /usr/bin, and the current directory
in that order). The current directory can be specified by
two or more adjacent colons, or by a colon at the beginning
or end of the path list. If the command name contains
a / (slash), then 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 a directory
or an a.out file, it is assumed to be a file containing
shell commands. A subshell is spawned to read it. All
nonexported aliases, functions, and named parameters are
removed in this case. If the shell command file does not
have read permission, or if the setuid and/or setgid bits
are set on the file, the shell executes an agent whose job
it is to set up the permissions and execute the shell with
the shell command file passed down as an open file. A
command in parentheses is executed in a subshell without
removing nonexported quantities.
Command Reentry [Toc] [Back]
The text of the last HISTSIZE (default 128) commands
entered from a terminal device is saved in a history file.
The $HOME/.sh_history file is used if the HISTFILE variable
is not set or is not writable. A shell can access
the commands of all interactive shells that use the same
named HISTFILE. The fc special command is used to list or
edit a portion of this file. The portion of the file to
be edited or listed can be selected by number or by giving
the first character or characters of the command. A single
command or range of commands can be specified. If you
do not specify an editor program as an argument to fc,
then the value of the FCEDIT parameter is used. If FCEDIT
is not defined, then /usr/bin/ed is used. The edited commands
are printed and reexecuted upon leaving the editor.
The editor name - (dash) is used to skip the editing phase
and to reexecute the command. In this case, a substitution
parameter of the form old=new can be used to modify
the command before execution. For example, if r is
aliased to 'fc -e -', then typing `r bad=good c' reexecutes
the most recent command, which starts with the letter
c, replacing the first occurrence of the string bad
with the string good.
Inline Editing Options [Toc] [Back]
Normally, each command line entered from a terminal device
is simply typed followed by a newline (<Return> or linefeed).
If the emacs, gmacs, or vi option is active, you
can edit the command line. To be in any of these edit
modes, set the corresponding option. An editing option is
automatically selected each time the VISUAL or EDITOR
variable is assigned a value ending in either of these
option names.
[Tru64 UNIX] The editing features require that the terminal
accept <Return> as carriage-return without linefeed
and that a space must overwrite the current character on
the screen. ADM terminal users should set the spaceadvance
switch to Space. Hewlett-Packard series 2621 terminal
users should set the straps to bcGHxZ etX.
[Tru64 UNIX] The editing modes create the impression that
the user is looking through a window at the current line.
The window width is the value of COLUMNS if it is defined,
otherwise it is 80 bytes. If the line is longer than the
window width minus 2, a mark is displayed at the end of
the window to notify the user. As the cursor moves and
reaches the window boundaries, the window is centered
about the cursor. The mark is a > (right angle bracket) if
the line extends on the right side of the window, a <
(left angle bracket) if the line extends on the left side
of the window, and an * (asterisk) if the line extends on
both sides of the window.
[Tru64 UNIX] The search commands in each edit mode provide
access to the history file. Only strings are
matched, not patterns, although if the leading character
in the string is a ^ (circumflex), the match is restricted
to begin at the first character in the line.
The emacs Editing Mode [Toc] [Back]
This mode is entered by enabling either the emacs or gmacs
option. The only difference between these two modes is
the way they handle <Ctrl-t>. To edit, the user moves the
cursor to the point needing correction and then inserts or
deletes characters or words as needed. All the editing
commands are control characters or escape sequences. The
notation for control characters is ^ (circumflex) followed
by the character. For example, ^F is the notation for
<Ctrl-f>. This is entered by pressing f while holding
down <Ctrl>. <Shift> is not depressed. (The notation ^?
indicates <Delete>.)
The notation for escape sequences is M- followed by a
character. For example, M-f (pronounced Meta f) is
entered by pressing <Esc> (ASCII 033) followed by f. (M-F
would be the notation for <Esc> followed by <Shift> (capital)
F.)
All edit commands operate from any place on the line (not
just at the beginning). Do not press <Return> or linefeed
after edit commands except when noted. Moves the cursor
forward (right) one character. Moves the cursor forward
one word. (The emacs editor's definition of a word is a
string of characters, consisting of only letters, digits,
and underscores, and delimited with spaces or tabs.)
Moves the cursor backward (left) one character. Moves the
cursor backward one word. Moves the cursor to the start
of the line. Moves the cursor to the end of the line.
Moves the cursor forward on the current line to the character
indicated by the character argument. Moves the cursor
backward on the current line to the character indicated
by the character argument. Interchanges the cursor
and mark. Deletes the previous character. (User-defined
Erase character as defined by the stty command, often
<Ctrl-h> or #.) Deletes the current character. Deletes
the current word. Deletes the previous word. Deletes the
previous word. Deletes the previous word (if your Interrupt
character is <Delete>, this command does not work).
Transposes the current character with next character in
emacs mode. Transposes two previous characters in gmacs
mode. Capitalizes the current character. Capitalizes the
current word. Changes the current word to lowercase.
Deletes from the cursor to the end of the line. If preceded
by a numerical parameter whose value is less than
the current cursor position, deletes from given position
up to the cursor. If preceded by a numerical parameter
whose value is greater than the current cursor position,
deletes from the cursor up to given cursor position.
Deletes from the cursor to the mark. Pushes the region
from the cursor to the mark on the stack. Kills the
entire current line. If two Kill characters are entered
in succession, all Kill characters from then on cause a
linefeed (useful when using paper terminals). (Userdefined
Kill character as defined by the stty command,
often <Ctrl-g> or @.) Restores the last item removed from
the line. (Yanks the item back to the line.) Performs a
linefeed and prints the current line. (Null character.)
Sets the mark. Sets the mark. Executes the current line
(newline). Executes the current line (enter). The Endof-File
character is processed as an End-of-File only if
the current line is null. Fetches the previous command.
Each time <Ctrl-p> is entered, the previous command back
in time is accessed. Moves back one line when not on the
first line of a multiline command. Fetches the least
recent (oldest) history line. Fetches the most recent
(youngest) history line. Fetches the next command line.
Each time <Ctrl-n> is entered, the next command line forward
in time is accessed. Reverses the search history for
a previous command line containing string. If an argument
of 0 (zero) is given, the search is forward. The string
variable is terminated by a <Return> or newline character.
If string is preceded by a ^ (circumflex), the matched
line must begin with string. If string is omitted, then
the next command line containing the most recent string is
accessed. In this case, an argument of 0 (zero) reverses
the direction of the search. Executes the current line
and fetches the next line relative to current line from
the history file. (Operate) Defines the numeric parameter
(escape). The digits are taken as an argument to the ne
|