ksh(1)ksh(1)NAMEksh - Korn shell
SYNOPSISksh [-ir] [-c command_string | -s] [+|-abCefhkmnopstuvx] | [+|-o]
[option...] [+|-A name] [argument...] | [file] [argument...]
The Korn shell is an interactive command interpreter and a command pro‐
gramming language.
OPTIONS
Causes ksh to read commands from command_string. Causes ksh to run as
an interactive shell. The SIGTERM signal is thus ignored, and the SIG‐
INT signal is caught, causing the current command to be terminated and
a new prompt to be output. Causes ksh to run as a restricted shell.
Causes ksh to read commands from standard input. If you do not specify
the -c option or do not specify any arguments to ksh other than
options, ksh automatically invokes the -s option. The -c option over‐
rides the -s option, however.
The rest of the options that can be used with ksh are described under
the set subcommand in the subsection Special ksh Commands.
DESCRIPTION
The Korn shell carries out commands either interactively from a termi‐
nal keyboard or from a file. The Korn shell is backward compatible
with the Bourne shell (invoked with the sh command) and contains virtu‐
ally all of the Bourne shell features, as well as several of the best
features of the C shell. See the sh(1) reference page for additional
instructions on shell invocation.
Some important features of the Korn 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 speci‐
fies the name of a command. Any remaining words, with a few excep‐
tions, are passed to that command. A space refers to both spaces and
tabs.
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 sig‐
nal. 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 | (ver‐
tical 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 ; (semi‐
colon), & (ampersand), && (two ampersands), or || (two vertical bars)
and optionally ended by a ; (semicolon), an & (ampersand), a |& (copro‐
cess), or a newline. These separators and terminators have the follow‐
ing 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 pre‐
ceding pipeline returns a nonzero exit value. Causes the list follow‐
ing it to be executed only if the preceding pipeline returns a 0 (zero)
exit value.
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-char‐
acter separators. An unquoted newline character following a pipeline
functions the same as a ; (semicolon).
Comments
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 new‐
line character.
Shell Flow Control Statements
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 Substi‐
tution.) 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 identifieris 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
End-of-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 sta‐
tus, executes the list following the first then. Otherwise, the list
following elif is executed and, if its value is 0 (zero), the list fol‐
lowing the next then is executed. Failing that, the else list is exe‐
cuted. 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; The keyword 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. Exe‐
cutes 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
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 sub‐
stitution.
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 invoca‐
tions of the shell. (See Invocation.)
Aliasing is performed when scripts are read, not while they are exe‐
cuted. 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 path names. An option
to the aliasing facility allows the value of the alias to be automati‐
cally set to the full path name of the corresponding command. These
aliases are called tracked aliases.
The value of a tracked alias is defined the first time the correspond‐
ing command is looked up and becomes undefined each time the PATH envi‐
ronment 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.
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
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 user name 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 vari‐
able assignment parameter begins with a tilde.
Command Substitution
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 Quot‐
ing.) 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 parenthe‐
ses.
Parameter Substitution
A parameter is an identifier, one or more digits, or any of the charac‐
ters *, @, #, ?, -, $, 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 val‐
ues 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 within [ ] (brackets). To assign
values to an array, use set -A name value... The value of all sub‐
scripts 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 with‐
out 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 spe‐
cial 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 inter‐
preted 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 parame‐
ters, 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. Substi‐
tutes 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 parameteris set and is non-
null; otherwise, substitute nothing. Causes the value of this substi‐
tution to be the value of parameter with the matched portion deleted if
the shell pattern matches the beginning of the value of parameter; oth‐
erwise 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 substi‐
tution 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 match‐
ing 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. options 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. Initially,
the value of _ (underscore) is an absolute path name 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. 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 num‐
ber 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 com‐
mand. The process number of the parent of the shell. The present
working directory set by the cd command. 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. This parameter is set by the
select statement and by the read special command when no arguments are
supplied. Each time this parameter is referenced, the number of sec‐
onds 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 ksh Commands) is turned on. If this parameter
is set, then parameter substitution is performed on the value to gener‐
ate the path name 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. 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
path name 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_COL‐
LATE, LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, and LC_TIME vari‐
ables also derive their settings from the locale set for LANG. Speci‐
fies the collating sequence to use when sorting names and when charac‐
ter 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 classi‐
fication information to use on your system. The default value is Amer‐
ican 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. 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. 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 sec‐
onds. Each file name can be followed by a ? (question mark) and a
message that is printed. The message will undergo parameter substitu‐
tion 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 pri‐
mary 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 #? (num‐
ber 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). The path name
of the shell is kept in the environment. 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
ksh Commands) is turned on.
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
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 argu‐
ments (those resulting from parameters that have no values) are
removed.
File name Generation
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 / char‐
acter itself, must be matched explicitly. In other instances of pat‐
tern 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
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 belong‐
ing 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 for‐
mat reference page for more information.
Quoting
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 func‐
tion names or special command names listed later cannot be altered by
quoting them.
Arithmetic Evaluation
An ability to perform integer arithmetic is provided with the let spe‐
cial command. Evaluations are performed using long arithmetic. Con‐
stants 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.
An arithmetic expression uses the same syntax, precedence, and associa‐
tivity 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 speci‐
fied 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 alterna‐
tive 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 arith‐
metic expression into the command line (that is, it is like a $ vari‐
able).
Prompting
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
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: TRUE, if file exists. TRUE, if file exists and
is a block-special file. TRUE, if file exists and is a character-spe‐
cial file. TRUE, if file exists and is a directory. TRUE, if file
exists. TRUE, if file exists and is an ordinary file. TRUE, if file
exists and has its setgid bit set. TRUE, if file exists and its group
matches the effective group ID of this process. TRUE, if file exists
and has its sticky bit set. TRUE, if file exists and is a symbolic
link. TRUE, if length of string is nonzero. TRUE, if option named
option is on. TRUE, if file exists and is owned by the effective user
ID of this process. TRUE, if file exists and is a FIFO special file or
a pipe. TRUE, if file exists and is readable by current process.
TRUE, if file exists and has size greater than 0 (zero). TRUE, if file
exists and is a socket. TRUE, if file descriptor number file_des is
open and associated with a terminal device. TRUE, if file exists and
has its setuid bit set. TRUE, if file exists and is writable by cur‐
rent process. 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. TRUE, if length of string
is 0 (zero). TRUE, if file1 exists and is newer than file2. TRUE, if
file1 exists and is older than file2. TRUE, if file1 and file2 exist
and refer to the same file. TRUE, if string matches pattern. TRUE, if
string does not match pattern. TRUE, if string1 collates before
string2. TRUE, if string1 collates after string2. TRUE, if expres‐
sion1 is equal to expression2. TRUE, if expression1 is not equal to
expression2. TRUE, if expression1 is less than expression2. TRUE, if
expression1 is greater than expression2. TRUE, if expression1 is less
than or equal to expression2. TRUE, if expression1 is greater than or
equal to expression2.
A compound expression can be constructed from these primitives by using
any of the following, listed in decreasing order of precedence. TRUE,
if expression is TRUE. Used to group expressions. TRUE if expression
is FALSE. TRUE, if expression1 and expression2 are both TRUE. 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; other‐
wise, parameter and command substitution occurs, \newline is ignored,
and \ must be used to quote the characters \, $, `, and the first char‐
acter of word. If - is appended to <<, then all leading tabs are
stripped from word and from the document. The standard input is dupli‐
cated 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 out‐
put 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 descrip‐
tor 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
The environment is a list of name-value pairs that is passed to an exe‐
cuted 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. Exe‐
cuted 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 origi‐
nally inherited by the shell, whose values can be modified by the cur‐
rent shell, plus any additions that must be noted in the export or
typeset -x commands.
When the value of an exported parameter is changed, the shell automati‐
cally exports the new value to all child processes. This behavior is
different from that of the Bourne shell, sh(1b), which does not auto‐
matically 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 exe‐
cution of command is concerned):
TERM=450 command argument ...
(export TERM; TERM=450; command argument ...)
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
This feature is intended for use with scripts written for early ver‐
sions of the shell; its use in new scripts is strongly discouraged. It
is likely to disappear someday.
Functions
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 spe‐
cial 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 com‐
mand.
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
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 num‐
bers. 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 SIGTSTP sig‐
nal to the current job. The shell then normally indicates that the job
has been stopped, and it prints another prompt. You can then manipu‐
late 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 sus‐
pension takes effect immediately, and corresponds to the Interrupt key
sequence in that pending output and unread input are discarded. A spe‐
cial 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 pre‐
pared 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 nor‐
mally 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
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 par‐
ent (but see also the trap command).
Execution
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 com‐
mand name is checked to see if it matches one of the user-defined func‐
tions. 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 func‐
tion 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 spe‐
cial command or a user-defined 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 con‐
taining 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 speci‐
fied 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 sub‐
shell without removing nonexported quantities.
Command Reentry
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 charac‐
ters 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=newcan 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
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 vari‐
able is assigned a value ending in either of these option names.
The editing features require that the terminal accept <Return> as car‐
riage-return without linefeed and that a space must overwrite the cur‐
rent character on the screen. ADM terminal users should set the space-
advance switch to Space. Hewlett-Packard series 2621 terminal users
should set the straps to bcGHxZ etX.
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.
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
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 nota‐
tion for control characters is ^ (circumflex) followed by the charac‐
ter. 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 char‐
acter indicated by the character argument. Moves the cursor backward
on the current line to the character indicated by the character argu‐
ment. Interchanges the cursor and mark. Deletes the previous charac‐
ter. (User-defined Erase character as defined by the stty command,
often <Ctrl-h> or #.) Deletes the current character. Deletes the cur‐
rent 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 cur‐
rent 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). (User-defined 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 End-of-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 com‐
mand line. Each time <Ctrl-n> is entered, the next command line for‐
ward 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 next
command. The commands that accept a parameter are <Ctrl-f>, <Ctrl-b>,
<Erase>, <Ctrl-c>, <Ctrl-d>, <Ctrl-k>, <Ctrl-r>, <Ctrl-p>, <Ctrl-n>,
<Ctrl-]>, <Esc-.>, <Esc-Ctrl-]>, <Esc-_>, <Esc-b>, <Esc-c>, <Esc-d>,
<Esc-f>, <Esc-h>, <Esc-l> and <Esc-Ctrl-h>. Your alias list is
searched for an alias by the name _letter and if an alias of this name
is defined, its value is inserted on the input queue. The letter
parameter must not be one of the preceding metafunctions. (Soft-key)
Your alias list is searched for an alias by the name __letter and if an
alias of this name is defined, its value is inserted on the input queue
(Soft-key). This can be used to program functions keys on many systems.
Same as <Esc-]> letter. The last word of the previous command is
inserted on the line. If preceded by a numeric parameter, the value of
this parameter determines which word to insert, rather than the last
word. Same as the <Esc-.> combination. Attempts file name generation
on the current word. An * (asterisk) is appended if the word does not
match any file or contain any special pattern characters. File name
completion. Replaces the current word with the longest common prefix
of all file names matching the current word with an asterisk appended.
If the match is unique, a / (slash) is appended if the file is a direc‐
tory, and a space is appended if the file is not a directory. Lists
the files matching current word pattern if an * (asterisk) were
appended. Multiplies the argument of the next command by four.
Escapes the next character. Editing characters, the user's Erase,
Kill, and Interrupt (normally by using <Delete>) characters can be
entered in a command line or in a search string if preceded by a \
(backslash). The backslash removes the next character's editing fea‐
tures (if any). Displays the version of the shell. Inserts a # (num‐
ber sign) at the beginning of the line and executes it. This causes a
comment to be inserted in the history file.
The vi Editing Mode
There are two typing modes. Initially, when you enter a command you
are in the input mode. To edit, the user enters control mode by typing
<Esc> (ASCII 033) and moves the cursor to the place needing correction
and then inserts or deletes characters or words as needed. Most con‐
trol commands accept an optional repeat count prior to the command.
When in vi mode on most systems, canonical processing is initially
enabled and the command is echoed again if the speed is 1200 baud or
greater, if it contains any control characters, or if less than 1 sec‐
ond has elapsed since the prompt was printed. The Escape character
terminates canonical processing for the remainder of the command and
the user can then modify the command line.
This scheme has the advantages of canonical processing with the type-
ahead echoing of raw mode. If the option viraw is also set, the termi‐
nal always has canonical processing disabled. This mode is implicit
for systems that do not support two alternate End-of-Line delimiters,
and can be helpful for certain terminals.
Input Edit Commands
By default the editor is in input mode. (User-defined Erase character
as defined by the stty command, often <Ctrl-h> or #.) Deletes the pre‐
vious character. Deletes the previous space-separated word. Termi‐
nates the shell. Escapes the next character. Editing characters and
the user's Erase or Kill characters can be entered in a command line or
in a search string if preceded by a <Ctrl-v>. A <Ctrl-v> removes the
next character's editing features (if any). Escapes the next Erase or
Kill character.
Motion Edit Commands
These commands move the cursor: Cursor forward (right) one character.
Cursor forward one word. A word is a string of characters delimited by
spaces or tabs. Cursor to the beginning of the next word that follows
a space. Cursor to the end of the word. Cursor to end of the current
space-delimited word. Cursor backward (left) one character. Cursor
backward one word. Cursor to the preceding space-delimited word. Cur‐
sor to the column count. Finds the next character c in the current
line. Finds the previous character c in the current line. Equivalent
to f followed by h. Equivalent to F followed by l. Repeats count
times, the last single character find command: f, F, t, or T. Reverses
the last single character find command count times. Cursor to the
start of the line. Cursor to the first nonspace character in the line.
Cursor to the end of the line.
Search Edit Commands
These commands access your command history. Fetches the previous com‐
mand. Each time k is entered, the previous command back in time is
accessed. Equivalent to k. Fetches the next command. Each time j is
entered, the next command forward in time is accessed. Equivalent to
j. Fetches the command number count. The default is the least recent
history command. Searches backward through history for a previous com‐
mand containing the specified string. The string variable is termi‐
nated by <Return> or a newline character. If string is preceded by a ^
(circumflex), the matched line must begin with string. If string is
null, the previous string is used. Same as / (slash) except that the
search is in the forward direction. Searches for next match of the
last pattern to the / or ? commands. Searches for next match of the
last pattern to the / or ? commands, but in reverse direction.
Searches the command history for the string entered by the previous /
command.
Text Modification Edit Commands
These commands modify the line. Enters input mode and enters text
after the current character. Appends text to the end of the line.
Equivalent to $a. Deletes the current character through the character
to which motion would move the cursor, and enters input mode. If
motion is c, the entire line is deleted and input mode is entered.
Deletes the current character through the end of line, and enters input
mode. Equivalent to c$. Equivalent to cc. Deletes the current char‐
acter through the end of line. Equivalent to d$. Deletes the current
character through the character to which motion would move. If motion
is d, the entire line is deleted. Enters input mode and inserts text
before the current character. Inserts text before the beginning of the
line. Equivalent to 0i. Places the previous text modification before
the cursor. Places the previous text modification after the cursor.
Enters input mode and replaces characters on the screen with the char‐
acters you type, overlay fashion. Replaces the count characters,
starting at the current cursor position with c and advancing the cur‐
sor. Deletes the current character. Deletes the preceding character.
Repeats the previous text modification command. Inverts the case of
the count characters, starting at the current cursor position and
advancing the cursor. Causes the count word of the previous command to
be appended and input mode entered. The last word is used if count is
omitted. Causes an * (asterisk) to be appended to the current word and
file name generation to be attempted. If no match is found, it rings
the bell. Otherwise, the word is replaced by the matching pattern and
input mode is entered. File name completion. Replaces the current
word with the longest common prefix of all file names matching the cur‐
rent word with an * (asterisk) appended. If the match is unique, a /
(slash) is appended if the file is a directory; a space is appended if
the file is not a directory.
Miscellaneous vi Commands
Yanks the current character through the character to which motion would
move the cursor and puts the characters into the delete buffer. The
text and cursor are unchanged. Yanks from current position to the end
of line. Equivalent to y$. Undoes the last text-modifying command.
Undoes all the text-modifying commands performed on the line. Returns
the command fc -e ${VISUAL:-${EDITOR:-vi}} count in the input buffer.
If count is omitted, the current line is used. Performs a linefeed and
prints the current line. Effective only in control mode. Executes the
current line, regardless of mode (newline). Executes the current line,
regardless of mode (enter). Sends the line after inserting a # (number
sign) in front of the line. Useful for causing the current line to be
inserted in the history without being executed. Lists the file names
that match the current word if an * (asterisk) is appended to it.
Searches the alias list for an alias by the name _letter . If an alias
of this name is defined, its value is inserted in the input queue for
processing.
Special ksh Commands
The following simple commands are executed in the shell process.
Input/output redirection is permitted. Unless otherwise indicated, the
output is written on file descriptor 1 and the exit status, when there
is no syntax error, is 0 (zero).
Commands that are indicated as command(1) or command(2) are treated
specially in the following ways: Parameter assignment lists that pre‐
cede the command remain in effect when the command completes. I/O
redirections are processed after parameter assignments. Errors cause a
script that contains the commands so marked to abort. Words, following
a command specified as command(2) that are in the format of a parameter
assignment, are expanded with the same rules as a parameter assignment.
This means that ~ (tilde) substitution is performed after the = (equal
sign). Word splitting and file name generation are not performed. The
command only expands arguments. Reads the complete file and executes
the commands. The commands are executed in the current shell environ‐
ment. The search path specified by PATH is used to find the directory
containing file. If any arguments are specified, they become the posi‐
tional parameters. Otherwise, the positional parameters are unchanged.
The exit status is the exit status of the last command executed. The
alias command with no arguments prints the list of aliases in the form
name=value on standard output. An alias is defined for each name whose
value is given. A trailing space in value causes the next word to be
checked for alias substitution. The -t option is used to set and list
tracked aliases. The value of a tracked alias is the full path name
corresponding to the given name. The value becomes undefined when the
value of PATH is reset but the aliases remained tracked. Without the
-t option, for each name in the argument list for which no value is
given, the name and value of the alias is printed. The -x option is
used to set or print exported aliases. An exported alias is defined
for scripts invoked by name. The exit status is nonzero if a name is
given without a value, and no alias was defined. Puts each specified
job into the background. The current job is put into the background if
job is not specified. (See Jobs for a description of the format of
job.) Exits from the enclosing for, while, until, or select loop, if
any. If n is specified, breaks n levels. Resumes the next iteration
of the enclosing for, while, until, or select loop. If n is specified,
resumes at the nth enclosing loop. This command can be in either of
two forms. In the first form, it changes the current directory to
argument. If argument is a - (dash), the directory is changed to the
previous directory. The HOME shell parameter is the default argument.
The PWD parameter is set to the current directory. The CDPATH shell
parameter defines the search path for the directory containing argu‐
ment. Alternative directory names are separated by a : (colon). The
default path is a null string, specifying the current directory. Note
that the current directory is specified by a null path name, which can
appear immediately after the = (equal sign) or between the colon delim‐
iters anywhere else in the path list.
If argument begins with a / (slash), the search path is not
used. Otherwise, each directory in the path is searched for
argument. The second form of cd substitutes the string new for
the string old in the current directory name PWD and tries to
change to this new directory. The cd command cannot be executed
by rsh. Writes arguments to standard output. The arguments are
read as input to the shell and the resulting commands are exe‐
cuted. If argument is given, the command specified by the argu‐
ments is executed in place of this shell without creating a new
process. Input/output arguments can appear and affect the cur‐
rent process. If no arguments are given, the effect of this
command is to modify file descriptors as prescribed by the
input/output redirection list. In this case, any file descrip‐
tor numbers greater than 2 that are opened with this mechanism
are closed when invoking another program. Causes the shell to
exit with the exit status specified by n. If n is omitted, the
exit status is that of the last command executed. An End-of-
File also causes the shell to exit, except for a shell which has
the ignoreeof option (see set) turned on. The given names are
marked for automatic export to the environment of subsequently
executed commands. The export -p command outputs the names and
values of all exported variables, one per line, in the format
export variable=value In the first two forms, a range of com‐
mands from first to last is selected from the last HISTSIZE com‐
mands that were entered at the terminal. The arguments first
and last can be specified as a number or as a string. A string
is used to locate the most recent command that starts with the
given string. A negative number is used as an offset to the
current command number.
In the first form the editor program editor is invoked on a file
containing these keyboard commands. In the second form, the com‐
mands are listed on standard output and no editor is invoked.
In the first form, if editor is not supplied, the value of the
parameter FCEDIT (default /usr/bin/ed) is used as the editor.
When editing is complete, the edited commands are executed. If
last is not specified, then it is set to first. If first is not
specified, the default is the previous command for editing and
-16 for listing. The -r option reverses the order of the com‐
mands and the -n option suppresses command numbers when listing.
In the third form, command is reexecuted, without invoking the
editor, after the optional substitution old=new is performed.
Each job specified is brought to the foreground. Otherwise, the
current job is brought into the foreground. (See Jobs for a
description of the format of job.) Checks argument for legal
options. If argument is omitted, the positional parameters are
used. An option argument begins with a + (plus sign) or a -
(dash). An option not beginning with + or - or the argument --
ends the options. The optstring special command contains the
letters that getopts recognizes. If a letter is followed by a :
(colon), that option is expected to have an argument. The
options can be separated from the argument by spaces. The
getopts special command places the next option letter it finds
inside variable name each time it is invoked with a + prepended
when argument begins with a +. The index of the next argument
is stored in OPTIND.
The option argument, if any, gets stored in OPTARG. A leading :
in optstring causes getopts to store the letter of an invalid
option in OPTARG, and to set name to a ? (question mark) for an
unknown option and to : when a required option is missing. Oth‐
erwise, getopts prints an error message. The exit status is
nonzero when there are no more options. Without the option,
equivalent to alias -t. The -r option empties the list of
tracked aliases. This command is no longer supported. See the
loader(5) reference page for information on using shared
libraries. Lists information about each given job; or all
active jobs if job is omitted. The -l option lists process IDs
in addition to the normal information. The -n option only dis‐
plays jobs that have stopped or exited since last notified. The
-p option causes only the process group to be listed. (See Jobs
for a description of the format of job.) Sends either the TERM
signal or the specified signal to the specified jobs or pro‐
cesses. Signals are either given by number or by names (as
given in /usr/include/signal.h, stripped of the prefix SIG). If
the signal being sent is TERM (terminate) or HUP (hangup), the
job or process is sent a CONT (continue) signal if it is
stopped. The argument job can be the process ID of a process
that is not a member of one of the active jobs. (See Jobs for a
description of the format of job.) In the second form, kill -l,
the signal numbers and names are listed. Each argument is a
separate arithmetic expression to be evaluated. (See Arithmetic
Evaluation for a description of arithmetic expression evalua‐
tion.) The exit status is 0 (zero) if the value of the last
expression is nonzero, and 1 otherwise. Changes the primary
group identification of the current shell process to group. If
you specify a - (dash), newgrp changes the login environment to
the login environment of the new group. If you do not specify a
group, newgrp changes the group identification to that specified
for the current user in the /etc/passwd file. The newgrp com‐
mand recognizes group names only; it does not recognize group ID
numbers.
Only a user with the anygroup command authorization can change
the primary group of the shell to one in which the user does not
have membership. Only a user with superuser authority can change
the primary group of the shell to one to which that user does
not belong.
Any active user-generated shell is terminated when the newgrp
command is used. The shell output mechanism. With no options
or with option - or --, the arguments are printed on standard
output as described by echo. In raw mode, -R or -r, the escape
conventions of echo are ignored. The -R option prints all sub‐
sequent arguments and options other than -n.
The -p option causes the arguments to be written to the pipe of
the process spawned with |& instead of standard output. The -s
option causes the arguments to be written onto the history file
instead of standard output. The -u option can be used to spec‐
ify a 1-digit file descriptor unit number n on which the output
is placed. The default is 1. If the -n option is used, no new‐
line is added to the output. Equivalent to print -r - $PWD.
The shell input mechanism. One line is read and is broken up
into fields using the characters in IFS as separators. In raw
mode, a \ (backslash) at the end of a line does not signify line
continuation. The first field is assigned to the first name,
the second field to the second name, and so on, with leftover
fields assigned to the last name. The -p option causes the
input line to be taken from the input pipe of a process spawned
by the shell using |&. If the -s option is present, the input
is saved as a command in the history file. The -u option can be
used to specify a 1-digit file descriptor unit to read from.
The file descriptor can be opened with the exec special command.
The default value of n is 0 (zero). If name is omitted, REPLY
is used as the default name. The exit status is 0 (zero) unless
an End-of-File is encountered. An End-of-File with the -p option
causes cleanup for this process so that another can be spawned.
If the first argument contains a ? (question mark), the remain‐
der of this word is used as a prompt on standard error when the
shell is interactive. The exit status is 0 (zero) unless an
End-of-File is encountered. The variables whose names are given
are marked read-only. These variables cannot be unset or changed
by subsequent assignment. The -p option outputs the names and
values of all readonly variables, one per line, in the format
readonly variable=value Causes a shell function to return to the
invoking script with the return status specified by n. If n is
omitted, the return status is that of the last command executed.
If return is invoked while not in a function or a (dot) script,
it is the same as an exit. This command is no longer supported.
See the loader(5) reference page for information on using shared
libraries. Using + rather than - causes these options to be
turned off. These options can also be used upon invocation of
the shell. The options for the set command have the following
meanings: Array assignment. Unsets the variable name and assign
values sequentially from the list argument. If +A is used, the
variable name is not unset first. Automatically exports subse‐
quent parameters that are defined. Causes the shell to notify
the user asynchronously of background job completions. Prevent
existing files from being overwritten by the shell's > redirect‐
ion operator; the >| redirection operator overrides this noclob‐
ber option for an individual file. If a command has a nonzero
exit status, executes the ERR trap, if set, and exits. This
mode is disabled while reading profiles. Disables file name
generation. Each command becomes a tracked alias when first
encountered. All parameter assignment arguments are placed in
the environment for a command, not just those that precede the
command name. Background jobs will run in a separate process
group and a line will print upon completion. The exit status of
background jobs is reported in a completion message. On systems
with job control, this option is turned on automatically for
interactive shells. Reads commands and checks them for syntax
errors, but does not execute them. Ignored for interactive
shells. The argument can be one of the following option names:
Same as a. Same as e. Runs all background jobs at a lower pri‐
ority. This is the default mode. Invokes an emacs-style inline
editor for command entry. Invokes a gmacs-style inline editor
for command entry. The shell does not exit on End-of-File. The
exit command must be used. Same as k. All directory names
resulting from file name generation have a trailing / (slash)
appended. Same as m. Prevents redirection > from truncating
existing files. Requires >| to truncate a file when turned on.
Same as n. Same as f. Does not save function definitions in
history file. Same as u. Same as p. Same as v. Same as h.
Invokes, in insert mode, a vi-style inline editor until you
press Escape (ASCII 033). This changes to move mode. A return
sends the line. Each character is processed as it is entered in
vi mode. Same as x.
If no option name is supplied, then the current option settings
are printed. Disables processing of the $HOME/.profile file and
uses the /etc/suid_profile file instead of the ENV file. This
mode is on whenever the effective user ID or group ID is not
equal to the real user ID or group ID. Turning this off causes
the effective user ID and group ID to be set to the real user ID
and group ID. Sorts the positional parameters. Exits after
reading and executing one command. Treats unset parameters as
an error when substituting. Prints shell input lines as they
are read. Prints commands and their arguments as they are exe‐
cuted. Unsets x and v flags and stops examining arguments for
options. Does not change any of the options; useful in setting
$1 to a value beginning with -. If no arguments follow this
option, the positional parameters are unset.
These options can also be used upon invocation of the shell.
The current set of options can be found in $-. Unless -A is
specified, the remaining arguments are positional parameters and
are assigned, in order, to $1 $2 .... If no arguments are
given, the names and values of all named parameters are printed
on the standard output. If the only argument is +, the names of
all named parameters are printed. The positional parameters
from $n+1 ... are renamed $1 ...; the default n is 1. The
argument n can be any arithmetic expression that evaluates to a
nonnegative number less than or equal to $#. Prints the accumu‐
lated user and system times for the shell and for processes run
from the shell. The argument variable specifies a command to be
read and executed when the shell receives the specified signals.
(Note that argument is scanned once when the trap is set and
once when the trap is taken.) Each signal can be given as a num‐
ber or as the name of the signal. Trap commands are executed in
order of signal number. Any attempt to set a trap on a signal
that was ignored on entry to the current shell is ineffective.
If argument is omitted or is -, all traps associated with signal
are reset to their original values. If argument is the null
string, signal is ignored by the shell and by the commands it
invokes. If signal is ERR, the command argument is executed
whenever a command has a nonzero exit status. If signal is
DEBUG, argument is executed after each command. If signal is 0
(zero) or EXIT and the trap statement is executed inside the
body of a function, the command argument is executed after the
function completes. If signal is 0 (zero) or EXIT for a trap
set outside any function, the command argument is executed on
exit from the shell. The trap command with no variables prints
a list of commands associated with each signal number.
Note
Although signal is an optional parameter, using argument without
specifying a value for signal will have no effect. This is not
considered an error. Sets attributes and values for shell
parameters. When invoked inside a function, a new instance of
the parameter name is created. The parameter value and type are
restored when the function completes. The following list of
attributes can be specified: The names refer to function names
rather than parameter names. No assignments can be made and the
only other valid options are -t, -u, and -x. The -t option
turns on execution tracing for this function. The -u option
causes this function to be marked undefined. The FPATH variable
is searched to find the function definition when the function is
referenced. The -x option allows the function definition to
remain in effect across shell procedures invoked by name. Pro‐
vides system-to-hostname file mapping on machines that restrict
the set of characters in file names. Parameter is an integer.
This makes arithmetic faster. If n is nonzero, it defines the
output arithmetic base; otherwise, the first assignment deter‐
mines the output base. All uppercase characters are converted
to lowercase. The uppercase -u option is turned off. Left jus‐
tifies and removes leading spaces from value. If n is nonzero,
it defines the width of the field; otherwise, it is determined
by the width of the value of first assignment. When the parame‐
ter is assigned, it is filled on the right with spaces or trun‐
cated, if necessary, to fit into the field. Leading zeros are
removed if the -Z option is also set. The -R option is turned
off. The given names are marked read-only and these names can‐
not be changed by subsequent assignment. Right justifies and
fills with leading spaces. If n is nonzero, it defines the
width of the field; otherwise, it is determined by the width of
the value of first assignment. The field is left-filled with
spaces or truncated from the end if the parameter is reassigned.
The L option is turned off. Tags the named parameters. Tags
are user definable and have no special meaning to the shell.
All lowercase characters are converted to uppercase characters.
The lowercase -l option is turned off. The given names are
marked for export. Right justifies and fills with leading zeros
if the first nonspace character is a digit and the -L option was
not set. If n is nonzero, it defines the width of the field;
otherwise, it is determined by the width of the value of first
assignment.
Using + (plus sign) rather than - (dash) causes these options to
be turned off. If no name arguments are given but options are
specified, a list of names (and optionally the values) of the
parameters that have these options set is printed. (Using +
rather than - keeps the values from being printed.) If no names
and options are given, the names and attributes of all parame‐
ters are printed. Sets or displays a resource limit. Available
resources limits follow. Many systems do not contain one or more
of these limits. The limit for a specified resource is set when
limit is specified. The value of limit can be a number in the
unit specified with each resource, or the value unlimited.
The H and S options specify whether the hard limit or the soft
limit for the given resource is set. A hard limit cannot be
increased once it is set. A soft limit can be increased up to
the value of the hard limit. If neither H nor S is specified,
the limit applies to both. The current resource limit is
printed when limit is omitted. In this case, the soft limit is
printed unless H is specified. When more than one resource is
specified, the limit name and unit are printed before the value.
Lists all of the current resource limits. The number of
512-byte blocks on the size of core dumps. The number of Kilo‐
bytes on the size of the data area. The number of 512-byte
blocks on files written by child processes (files of any size
can be read). The number of Kilobytes on the size of physical
memory. The number of file descriptors. The number of Kilo‐
bytes on the size of the stack area. The number of seconds to
be used by each process. The number of Kilobytes for virtual
memory.
Note
This option is supported only if RLIMIT_VMEM has been defined in
/usr/include/sys/resource.h. The number of Kilobytes for the
swap area.
Note
This option is supported only if RLIMIT_SWAP has been defined in
/usr/include/sys/resource.h.
If no option is given, -f is assumed. The user file-creation
mask is set to mask (See umask.) The mask variable can either
be an octal number or a symbolic value as described in chmod. If
a symbolic value is given, the new umask value is the complement
of the result of applying mask to the complement of the previous
umask value. If mask is omitted, the current value of the mask
is printed. Produces Symbolic output The parameters given by
the list of names are removed from the alias list. The unalias
-a command removes all aliases from the current shell execution
environment. The variables or functions given by the list of
names are unassigned, that is, their values and attributes are
erased. Read-only variables cannot be unset. If the -f option
is specified, the names refer to function names. If no options
or the -v option is specified, the names refer to variables.
Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND, RANDOM, SEC‐
ONDS, TMOUT, and _ removes their special meaning even if they
are subsequently assigned. Waits for the specified job and
reports its termination status. If job is not given, all cur‐
rently active child processes are waited for. The exit status
from this command is that of the process waited for. (See Jobs
for a description of the format of job.) For each name, indi‐
cates how it would be interpreted if used as a command name.
The -v option produces a more verbose report. The -p option
does a path search for name even if name is an alias, a func‐
tion, or a reserved word.
Invocation
If the shell is invoked by exec, and the first character of argument
zero ($0) is - (dash), the shell is assumed to be a login shell and
commands are read from /etc/profile and then from either in the current
directory or $HOME/.profile, if either file exists. Next, commands are
read from the file named by performing parameter substitution on the
value of the ENV environment variable, if the file exists. If the -s
option is not present and argument is present, a path search is per‐
formed on the first argument to determine the name of the script to
execute. The script argument must have read permission and any setuid
and getgid settings are ignored. Commands are then read, as described
in the following text.
See the OPTIONS section for a complete description of options that can
be interpreted by the shell when it is invoked.
NOTES
If a command is executed, and a command with the same name is installed
in a directory in the search path before the directory where the origi‐
nal command was found, the shell will execute the original command.
Use the hash command to correct this situation. When the shell encoun‐
ters the >> characters, it does not open the file in append mode;
instead, the shell opens the file for writing and seeks to the end.
Failure (nonzero exit status) of a special command preceding a || sym‐
bol prevents the list following || from executing. If a command that
is a tracked alias is executed, and then a command with the same name
is installed in a directory in the search path before the directory
where the original command was found, the shell continues to exec the
original command. Use the -t option of the alias command to correct
this situation. Using the fc built-in command within a compound com‐
mand causes the whole command to disappear from the history file. The
built-in command reads the whole file before any commands are executed.
Therefore, the alias and unalias commands in the file do not apply to
any functions defined in the file. Traps are not processed while a job
is waiting for a foreground process. Thus, a trap on CHLD is not exe‐
cuted until the foreground job terminates.
EXIT STATUS
Errors detected by the shell, such as syntax errors, cause the shell to
return a nonzero exit status. Otherwise, the shell returns the exit
status of the last command executed. (See also the exit command,
described previously.) If the shell is being used noninteractively,
execution of the shell file is abandoned. Run-time errors detected by
the shell are reported by printing the command or function name and the
error condition. If the line number that the error occurred on is
greater than 1, the line number is also printed in [ ] (brackets) after
the command or function name.
FILES
System profile User profile Contains user information
SEE ALSO
Commands: alias(1), bg(1), cat(1), cd(1), chmod(1), csh(1), echo(1),
env(1), fg(1), hash(1), jobs(1), kill(1), newgrp(1), nice(1), nohup(1),
sh(1), Bourne shell sh(1b), POSIX shell sh(1p), stty(1), test(1),
time(1), ulimit(1), umask(1), unalias(1), vi(1), wait(1)
Functions: exec(2), fcntl(2), fork(2), ioctl(2), lseek(2), pipe(2),
read(2), sigaction(2), signal(2), umask(2), wait(2)
Routines: rand(3), ulimit(3)
Files: null(7)
Miscellaneous: loader(5)ksh(1)