sh, resh, kshPOSIX-compliant (Korn) shell (command interpreter) |
Command |
sh
[±abCefGHhiKkLmnprtuvwx
]
[±o
option]
[cmd_file [argument...]]
sh
[±abCefGHhiKkLmnprtuvwx
]
[±
option]
-c
cmdstring
[cmd_name [argument...]]
sh
-s
[±abCefGHhiKkLmnprtuvwx
]
[±o
option]
[argument...]
resh
[±abCefGHhiKkLmnptuvwx
]
[±o
option]
[cmd_file [argument...]]
resh
[±abCefGHhiKkLmnptuvwx
]
[±
option]
-c
cmdstring
[cmd_name [argument...]]
resh
-s
[±abCefGHhiKkLmnptuvwx
]
[±o
option]
[argument...]
ksh
[±abCefGHhikLmnprtuvwx
]
[±o
option]
[cmd_file [argument...]]
ksh
[±abCefGHhikLmnprtuvwx
]
[±
option]
-c
cmdstring
[cmd_name [argument...]]
ksh
-s
[±abCefGHhikLmnprtuvwx
]
[±o
option]
[argument...]
sh
, resh
,
and ksh
. sh
is a POSIX.2-compliant shell
based on the KornShell, while resh
is a restricted version
of that shell (as discussed in the description of the -r
option), and ksh
is a version of the shell that uses, by
default, standard KornShell behavior over POSIX.2 behavior.
resh
is equivalent to running sh
-r
. ksh
is equivalent to running
sh
with the -K
or -o
korn
options.
This page contains the following subsections:
cd
and
alias
. The reference pages for such
commands describe additional features of the shell not included here.
sh
) is a sophisticated shell which
is upwardly compatible with the Bourne Shell.
The shell can be run interactively or as a script processor. Since interactive
use is most common, it is described first. Normally you invoke the shell by
logging in. You can also invoke the shell by typing an explicit
sh
command.
If you invoke the shell with a name that begins with the -
character, it is invoked as a login shell which executes any
profile files you have set up. (You can also get a login shell if you invoke the
shell with the -L
option.) You can use profile files to
customize your session with sh
. For example, your profile
files can set options, create aliases, or define functions and variables.
A login shell begins by executing the file
It then executes/etc/profile
using the$HOME/.profile
.
command (see
dot
). If
$HOME
is not set, the shell searches the current
directory for
and executes this file with the.profile
.
command, if it exists.
The shell does not complain if it cannot find any of these files.
If there is at least one argument on the sh
command line,
sh
takes the first argument as the name of a shell script to
execute (the exception to this is when -s
is used). Any
additional arguments are assigned to the positional parameters, which usually
serve as arguments to the shell script. See
Parameter Substitution for
information about positional parameters, and
set
for information about changing
these parameters.
If sh
finds the ENV
environment
variable set when it begins execution (after profile processing), it executes
the file named by the expansion of the value of this variable (see
Variables).
-c
cmdstringexecutes cmdstring as if it were an input line to the shell and
then exits. This is used by programs (for example, editors) that call the
shell for a single command. sh
assigns arguments in
cmdstring to the positional parameters. If you specify
cmd_name, special parameter 0 is set to this string for use when
executing the commands in cmdstring.
sh
ignores any command line options that
follow -c
on the command line.
-G
always writes shell errors to standard error and turns off all GUI
features for the running shell. If this option is specified, it overrides
set
-o guierror
.
-H
starts the shell in hidden mode. This option is useful when running graphical functions from shell scripts.
-i
invokes an interactive shell, as opposed to running a script. With
-i
, the shell catches and ignores interrupts. Without
-i
, an interrupt terminates the shell. For shells which
read from the terminal, -i
is the default.
-K
specifies Korn-compatible behavior in any case where the POSIX.2
behavior is different from the behavior specified by Korn. Without
-K
, the shell defaults to POSIX.2 behavior.
-L
makes the shell a login shell as described earlier under Options and Invocation.
-r
invokes a restricted shell (as noted earlier, you can also invoke a
restricted shell by using the name resh
). In a
restricted shell, you cannot do any of the following: use the
cd
command; change the values of
the variables ENV
,
PATH
, or SHELL
; use
>
or >>
to redirect output; specify
command names containing /
. These restrictions do not apply
during execution of profile files.
-w
when exiting, displays [Hit return to continue]
on the
console and waits for the user to do so before closing the console.
-s
reads commands from standard input and assigns all arguments to the positional parameters. Normally, if there is at least one argument to the shell, the first such argument is the name of a file to execute.
-c
or
-s
option, the shell takes it as the name of a file
containing commands to be run. Special parameter 0 is set to this name.
In addition, you can use the -a
, -b
,
-C
, -e
, -f
,
-h
, -i
, -k
,
-L
, -m
, -n
,
-o
, -p
, -r
-t
, -u
, -v
,
and -x
options. You can specify these options with either a
-
to turn them off or a +
to turn them on. The
set
reference page describes these
options.
To use any of these characters inside an actual argument, you must quote the argument (so that the shell doesn't use the special meanings of the characters). See Quoting for more information. A simple command is a list of arguments separated by blanks or tabs. When a word begins with an unescaped hash mark (; & ( ) < > | ` \ \" ' space tab newline
#
), the remainder
of the line is treated as a comment and the shell discards input up to
but not including the next newline.
When a command starts with a defined alias, sh
replaces the
alias with its definition (see alias
).
A reserved word command starts with a reserved word (for example,
if
, while
, for
).
Reserved word commands provide flow of control operations for the shell. These
are described later in this section.
A command may be any of the following:
The following table gives the order of precedence of the preceding operators. Highest priority operators are listed first and operators on the same line have equal priority.command: simple command reserved word command(
command)
command|
command command&&
command command||
command command&
command command&
command|&
command;
command command;
command <newline>
You can also combine commands into a structure known as a compound-command which is simply a series of individuals commands joined together by these operators. The following list shows the meaning of these operations.() | && || & |& ; <newline>
(
compound-commandexecutes the commands in compound-command in a subshell. This
means that the current shell invokes a second shell to execute the
commands. In this way, compound-command executes in a completely
separate execution environment; it can change working directories, change
variables, open files, and so on, without affecting the first shell. The
subshell's environment begins as a copy of the current environment, so the
value of the ENV
environment variable is not
used when a subshell starts.
|
creates a pipe between the two commands that the |
operator connects. This means that the standard output of the first
command becomes the standard input of the second command.
A series of commands connected by pipes is called a pipeline.
The exit status is that of the last command in the pipeline.
&&
is the logical AND operator. The shell executes the second command -- if and only if -- the first command returns a true (zero) exit status.
||
This is the logical OR operator. The shell executes the second command -- if and only if -- the first command returns a false (non-zero) exit status.
&
asynchronously executes the command that precedes it. This means that the shell just starts the command running and then immediately goes on to take new input, before the command finishes execution.
|&
executes the command that precedes it as a co-process.
The command runs asynchronously, as with the &
operator, but command's standard input and standard output are
connected to the shell by pipes. The shell sends input to command's
standard input with the print
-p
command and reads from command's standard
output with the read
-p
command. The command should not buffer its
output. Because of this and other limitations, a program intended to be
used as a co-process must be designed as such.
;
is the sequential execution operator. The second command is executed only after the first command has been completed.
the unescaped newline is equivalent to the ;
operator.
!
commandThe exclamation point is the logical NOT command. When its operand is false (non-zero), this command returns true (zero). When its operand is true (zero), this command returns false (non-zero).
{
compound-command;}
Enclosing a command in braces is similar to the
(
compound-command construct except that the shell
executes the compound-command in the same environment rather than
under a subshell. {
and }
are simply reserved
words to the shell. To make it possible for the shell to recognize these
symbols you must put a blank or newline after the {
and a
semicolon or newline before the }
.
case
word in
|
pattern]
...compound-command ;;
] ...|
pattern]
...compound-command ;;
] ...esac
The case
statement is similar to the
switch
statement of the C programming language or the
case
statement of Pascal. If the given word
matches any one of the patterns separated by or-bar (|
)
characters, sh
executes the corresponding
compound-command. The patterns follow the rules given in
File Name Generation except
that the period (.
) and slash (/
) are not
treated specially. Patterns are matched in the order they are given, so
more inclusive patterns should be mentioned later. Once a pattern matching
word has been found, no further patterns are expanded. You must use
the double semicolon (;;
) to delimit compound-command
and introduce the next pattern.
By default, pattern-matching (like file name generation) in
case
statements is case-insensitive. To make
pattern-matching case-sensitive,
the DUALCASE
environment variable determines
whether or not pattern-matching is case-sensitive in the same way it
does for file name generation.
for
variable [in
word ...]do
compound-commanddone
The for
statement sets variable to each
word argument in turn, and executes the set of commands once
for each setting of variable. If you omit the in
word part, sh
sets variable to each
positional parameter. You may divert the flow of control within the loop
with the break
or
continue
statements.
The exit status of a for
command is the exit status of
the last command in the loop to execute.
If the loop contains no commands, the exit status is zero.
function
funcname {
}
() {
}
Either of these forms defines a function named funcname, the
body of which consists of the sequence of commands. You invoke a
function just like any other command; when you actually call the function,
sh
saves the current positional parameters. The
function's command line arguments then replace these parameters until the
function finishes. If the option flag -K
is set,
sh
also saves the current ERR
and
EXIT
traps and any flags manipulated with the
set
command; these are restored
when the function finishes. The function terminates either by falling off
the end of the code of the function body, or by reaching a
return
statement. If the
function uses typeset
to
declare any variables in the function body, the variables are local to the
function.
The exit status of a function definition is zero, if the function was declared successfully; otherwise, it is greater than zero. The exit status of an invoked function is the exit status of the last command executed by the function.
if
compound-commandthen
compound-commandelif
compound-commandthen
compound-command] ...else
compound-command]fi
In the if
statement, if the first (leftmost)
compound-command succeeds (returns a zero exit status),
sh
executes the compound-command following
then
; otherwise, sh
executes the
compound-command (if any) following the elif
(which is short for "else if"); if that succeeds, sh
executes the compound-command following the next
then
. If none of these cases hold,
sh
executes the compound-command following the
else
(if any).
if
command is the exit status of
the then
compound-command or
else
compound-command that was executed, or
zero, if none was executed.select
variable [in
word ...]do
compound-commanddone
The select
statement can handle menu-like
interactions with the user. Its syntax is like the for
statement. Each word is printed on the standard error file, one per
line, with an accompanying number. If you omit the "in
word..." part, sh
uses the positional
parameters. sh
then displays the value of the variable
PS3
to prompt the user to enter a numerical
reply. If the reply is an empty line, sh
displays the
menu again; otherwise, sh
assigns the input line to
the variable REPLY
, sets variable to the
word selected, then executes the compound-command.
sh
does this over and over until the loop is
terminated by interrupt, end-of-file, or an explicit
break
statement in the
compound-command.
until
compound-command1do
compound-command2done
The until
statement executes
compound-command1 and tests its exit status for success (zero) or
failure (non-zero). If command1 succeeds, the loop terminates;
otherwise, sh
executes compound-command2, then
goes back to execute and test compound-command1 again. Including
break
or
continue
commands in
compound-command2 can affect the operation of the loop.
until
command is the exit status
of the last compound-command2 executed, or zero if none was
executed.while
compound-command1do
compound-command2done
The while
statement works like the
until
statement; however, the loop terminates whenever
compound-command1 is unsuccessful (non-zero exit status).
while
command is the exit status
of the last compound-command2 executed, or zero if none was
executed.! { } case do done elif else esac fi for function if select then until while
is a simple command with one variable assignment, two arguments, and a redirection. The command is processed as follows:variable=value argument0 argument1 <filename
sh
performs word expansion on command arguments (see
Word Expansion). The first word of the
expanded arguments is the command name. If there are no arguments,
sh
only performs variable assignments and temporary
redirection.sh
performs word expansion on variable assignments. If
there is no command or the command is a special built-in command (see
Built-in Commands), variable
assignments affect the current environment; otherwise, variable assignments
affect the execution environment of the command.sh
does redirection, performing word expansion on any
file names (see File
Descriptors and Redirection).sh
next searches for the command name and executes the
command. If the command name is a special built-in command,
sh
invokes it. Most errors in special built-ins cause a
non-interactive shell to exit.
If the command name is a function, sh
executes the function.
You can disable the search for functions with the built-in command named
command
.
If the command name is a regular built-in command, sh
invokes it.
If the command name is not a regular or special built-in command or a function,
sh
searches for an executable file containing a shell script
or a program. The shell uses one of the following two methods to locate this
file.
/
)
characters in its name, the command is taken to be a full path name
(absolute or relative). The shell tries to execute the contents of that
file.PATH
variable. The value
should be a list of directory names. sh
searches under
each directory for a file name that matches the command name and executes
the first matching file found.sh
cannot execute the program and it is not a shell script,
the exit status is 126.
When the HASHBANG
environment variable is set and
the first line of a KornShell script file is of the form
command is executed with a command line consisting of arguments followed by the path name of the script file. If command has a UNIX-style path name (starts with a forward slash and has no file name extension) and cannot be found, then, for portability reasons, the shell attempts a path search on the basename of command. For example, suppose the file#![command] [arguments]
script
has the first line
Typing#!/usr/bin/perl
script
on the MKS KornShell command line executes the script
using the perl
command.
Command names may be marked as tracked aliases. The first time you execute a
command with a tracked alias, the shell does a normal
PATH
search. If the search is successful, the shell
remembers the file it finds. The next time you execute a command with the same
name, sh
immediately executes the file found on the last
PATH
search; there is no new search.
This speeds up the search for the appropriate file.
The set
-h
command
tells the shell that all commands should be treated as tracked aliases. See
alias
and
set
for more information.
sh
performs word expansion for simple commands, some
reserved word commands, redirection file names, some shell variables
(ENV
, MAILPATH
,
PS1
, and PS4
), and
unquoted here documents. There are four steps to expansion:
substitution, word splitting, path expansion, and quote removal.
Directory substitution, parameter substitution, command substitution, and
arithmetic substitution are each described in the appropriate section of
this reference page.
After substitution, each word which underwent parameter substitution is checked
for the characters in the IFS
variable (by default,
space, tab, and newline). If the word contains these characters, it is split
into multiple words. When IFS
contains an empty
string, this word splitting is not performed.
If a word contains an unquoted *
, ?
, or ]
or an an unquoted ?
, +
, @
, !
,
or *
followed by an open parentheses ((
), that word is
subject to path name expansion (see File Name
Generation). Path name expansion is disabled if the
-f
or -o
noglob
shell flag is set (see set
).
Finally, sh
removes any quote characters (\
,
'
, and "
) from the original word. In here
documents (see the description of
number<<[-]
name in the section
File Descriptors and
Redirection) and shell variables that expand, single and double quotes
have no special meaning and are not removed.
sh
may or may not do all of these steps, depending on where
word expansion is performed. Table 1, Word
Expansion summarizes what steps are done. The Arguments
entry refers to the word list of for
and
select
commands as well as simple commands.
Directory | Other | Word | Path | |
Substitution | Substitution | Splitting | Expansion | |
Arguments | yes | yes | yes | yes |
Assignments | yes | yes | no | no |
Redirection | yes | yes | no | yes/no* |
Here documents | no | yes | no | no |
Case statement | yes | yes | no | no |
Shell variables | no | yes | no | no |
* yes for interactive shell, no otherwise |
Table 1: Word Expansion
~
),
sh
tries to perform directory substitution on the
word. sh
obtains all characters from the tilde
(~
) to the first backslash \
(/
on UNIX)
and uses this as a user name. sh
looks for this
name in the user database. If sh
finds a matching
name, it replaces ~
name with the name of the user's home
directory, as given in the matching user database entry.
For example, if you specify a file name as
~jsmith/file
sh
would look up jsmith's home directory and put that
directory name in place of the ~jsmith
construct.
If you just specify a ~
without an accompanying name,
sh
replaces the ~
with the current value of
your HOME
variable (see
Variables). For example,
displays the name of your home directory. Similarly,echo ~
sh
replaces the construct tilde plus (~+
) with the value of the
PWD
variable (the name of the your current directory),
and replaces tilde hyphen (~-
) with the value of
OLDPWD
(the name of your previous current directory).
In variable assignments, tilde expansion is also performed after colons
(:
).
sh
.
A special parameter is represented with one of the following characters:
The values to which special parameters expand are listed later in this section. Variables are named parameters. For details on naming and declaring variables, see Variables. The simplest way to use a parameter in a command line is to enter a dollar sign (* @ # ? ! - $ 0
$
) followed by the name of the parameter.
For example, if you enter the command
echo $x
sh
replaces $x
with the value of the parameter
x
and then displays the results (because
echo
displays its arguments). Other
ways to expand parameters are shown later in this section.
Some parameters are built-in to the shell. These are as follows:
$1
, $2
, ... $9
expands to the dth positional parameter (where d is the
single digit following the $
. If there is no such parameter,
$
d expands to a null string.
$0
expands to the name of the shell, the shell script, or a value assigned when you invoked the shell.
$#
expands to the number of positional parameters. The parameter assigned
to $0
is not counted in this number, since it is a special
parameter, not a positional parameter.
$@
expands to the complete list of positional parameters. If
$@
is quoted, the result is separate arguments, each quoted.
This means that
is equivalent to"$@"
"$1" "$2" ...
$*
expands to the complete list of positional parameters. If
$*
is quoted, the result is concatenated into a single
argument, with parameters separated by the first character of the value of
IFS
(see
Variables). For example, if the first
character of IFS
is a space
is equivalent to"$*"
If"$1 $2 ..."
IFS
is unset, the parameters are separated
by a space.
Note that setting IFS
is not the same as
unsetting it. In this case, the parameters are separated by the null
string and as a result, concatenated.$-
expands to all options that are in effect from previous calls to the
set
command and from options on
the sh
command line.
$?
expands to the exit status of the last command executed.
$$
expands to the process number of the original parent shell.
$!
expands to the process number of the last asynchronous command.
$
in front of the parameter name. More sophisticated ways to expand
parameters are:
${
parameter}
expands any parameter.
${
number}
expands to the positional parameter with the given number. (When using
$
d to refer to the dth positional parameter,
d must be a single digit; with brace brackets, number can
be greater than nine.) Since braces mark the beginning and end of the name,
you can immediately follow the expression with a letter or digit.
${
variable[
arithmetic
expression]}
expands to the value of an element in an array named variable. The arithmetic expression gives the subscript of the array. (See Arithmetic Substitution.)
${
variable[*]}
expands to all elements in the array variable,
separated by the first character in IFS
.
${
variable[@]}
when unquoted, is the same as
${
variable[*]}
. When quoted as
"${
variable[@]}
", it expands to all the
elements in the array variable, each quoted individually.
${#
parameter}
expands to the number of characters in the value of the given parameter.
${#
*}
, ${#@}
expands to the number of positional parameters.
${#
variable[*]}
expands to the number of elements in the array named variable.
Elements that do not have assigned values do not count. For example, if
you only assign values to elements 0
and 4
, the
number of elements is 2
. Elements 1
through
3
do not count.
${
parameter:-
word}
expands to the value of parameter if it is defined and has a non-empty value; otherwise, it expands word. This means that you can use word as a default value if the parameter isn't defined.
${
parameter-
word}
is similar to the preceding construct, except that the parameter is expanded, if defined, even if the value is empty.
${
variable:=
word}
expands word with parameter expansion and assigns the result to variable, provided that variable is not defined or has an empty value. The result is the expansion of variable, whether or not word was expanded.
${
variable=
word}
is similar to the preceding construct, except that the variable must be undefined (it can't just be null) for word to be expanded.
${
parameter:?
word}
expands to the value of parameter provided it is defined and
non-empty. If parameter isn't defined or is null,
sh
expands and displays word as a message. If
word is empty, sh
displays a default message.
Once a non-interactive shell has displayed a message, it is terminated.
${
parameter?
word}
is similar to the preceding construct, except that sh
displays word only if parameter is undefined.
${
parameter:+
word}
expands word, provided that parameter is defined and non-empty.
${
parameter+
word}
expands word, provided that parameter is defined.
${
parameter#
pattern}
attempts to match pattern against the value of the specified
parameter. The pattern is the same as a case pattern.
sh
searches for the shortest prefix of the value of
parameter that matches pattern. If sh
finds no match, the previous construct expands to the value of
parameter; otherwise, the portion of the value that matched
pattern is deleted from the expansion.
${
parameter##
pattern}
is similar to the preceding construct, except that sh
deletes the longest prefix that matches pattern if it finds such a
match.
${
parameter%
pattern}
searches for the shortest suffix of the value of parameter matching pattern and deletes the matching string from the expansion.
${
parameter%%
pattern}
is similar to the preceding construct, except that sh
deletes the longest suffix that matches pattern if it finds
such a match.
${
parameter:
offset:
len}
${
parameter:
offset}
expands to a substring of parameter which has a length of
len characters beginning at the character position indicated by
offset (the first character is offset 0). If you omit
:
len or offset+len is greater than the
length of parameter, the substring ends with the last character
of parameter. Finally, if offset is greater than the length
of parameter, this construct expands to a null string.
${
parameter/
pat/
str}
expands to a string which is the value of parameter with the
first occurrence of the pattern pat replaced by the string
str. If you omit /
str or str, the matched
characters are deleted.
${
parameter//
pat/
str}
expands to a string which is the value of parameter with each
occurrence of the pattern pat replaced by the string str. If
you omit /
str or str, the matching characters
are deleted.
${
parameter/#
pat/
str}
expands to a string which is the value of parameter where an
occurrence pattern pat which matches the beginning characters of
parameter's value is replaced by the string str. If you
omit /
str or str, the matching characters are
deleted.
${
parameter/%
pat/
str}
expands to a string which is the value of parameter where an
occurrence pattern pat which matches the final characters of
parameter's value is replaced by the string str. If you
omit /
str or str, the matching characters are
deleted.
or$((arithmetic expression))
$[arithmetic expression]
sh
replaces this sequence with the value of
arithmetic expression. Arithmetic expressions consist of expanded
variables, numeric constants, and operators.
Numeric constants have the form
where the optional base is a decimal integer between 2 and 36 inclusive, and number is a non-negative number in the given base. The default base is 10. Undefined variables evaluate to zero. Table 2, Shell Operators, lists the operators in decreasing order of precedence. Operators sharing a heading have the same precedence. Evaluation within a precedence group is from left to right, except for the assignment operator which evaluates from right to left. Arithmetic expressions may be used without the enclosing[base#]number
$((
and
)
in assignment to an integer variable (see
typeset
); as an argument to the
following built-in commands:
break
continue
exit
let
return
shift
Summary of Operators | |
---|---|
Unary Operators | |
var++ ++ var | postfix/prefix increment |
var-- -- var | postfix/prefix decrement |
- | unary minus |
! | logical negation |
+ ~ | identity, bitwise negation |
Multiplicative Operators | |
* / % | multiplication, division, remainder |
Additive Operators | |
+ - | addition, subtraction |
Bitwise Shift Operators | |
<< >> | bitwise shift right, bitwise shift left |
Relational Operators | |
< > | less than, greater than |
<= >= | less than or equal, greater than or equal |
== != | equal to, not equal to |
Bitwise And Operator | |
& | and |
Bitwise Exclusive Or Operator | |
^ | exclusive or |
Bitwise Inclusive Or Operator | |
| | inclusive or |
Logical And Operator | |
&& | logical and |
Logical Or Operator | |
|| | logical or |
Conditional Operator | |
? : | if-else |
Assignment Operator | |
= *= /= %= | assignment |
+= -= <<= | |
>>= &= ^= |= |
Table 2: Shell Operators
sh
uses the expansion
of the standard output of one command in the command line for a second command.
There are two syntaxes.
The first syntax (called backquoting) surrounds a command with
grave accents (`
), as in
To process this command line,ls -l `cat list`
sh
first executes the
cat
command and collects its standard
output. The shell then breaks this output into arguments and puts the result
into the command line of the ls
command.
The previous command therefore lists the attributes of all files, the names of
which are contained in the file list
.
This syntax is easy to type, but is not useful if you want to put one command
substitution inside another (nesting command substitutions).
A more useful syntax is
as in$(command)
This command usesvi $(fgrep -l function $(find . -name '*.c'))
find
to search the
current directory and its subdirectories to find all files, the names of which
end in .c
. It then uses
fgrep
to search each such file for
those that contain the string function
. Finally, it calls Vi to
edit each such file.
There is an historical inconsistency in the backquoting syntax. A backslash
(\
) within a backquoted command is interpreted differently
depending on its context. Backslashes are interpreted literally unless they
precede a dollar sign ($
), grave accent (`
), or
another backslash (\
); in these cases, the leading backslash
becomes an escape character to force the literal interpretation of the
$
, `
, or \
. Consequently, the command
issued at system level produces the outputecho '\$x'
while the same command nested in a backquoted syntax\$x
produces the outputecho `echo '\$x'`
For improved readability, the$x
$(
command syntax is
recommended for command substitutions.
sh
performs command substitutions as if a new copy of the
shell had been invoked to execute the command. This affects the behavior of
$-
(standing for the list of options passed to the shell). If a
command substitution contains $-
, the expansion of $-
does not include the -i
option, since the command is being
executed by a non-interactive shell.
\
) in front of the character. This is called
escaping the character.
You can also use this method to tell the shell to disregard the special meaning
of the newline character by putting a backslash at the end of a line. The shell
ignores the escaped newline and joins the next line of input to the end of the
current line. In this way, you can enter long lines in a convenient and readable
fashion.
Escaping characters by putting a backslash in front of them is the most direct
way of telling the shell to disregard special meanings; however, it can be
awkward and confusing if you have several characters to escape.
As an alternative, you can put arguments in various types of quotes. Different
quote characters have different strengths. The apostrophe (single quote)
characters are the strongest. When you enclose a command line argument in
apostrophes, the shell disregards the special meanings of everything they
contain. For example,
displays the * character, rather than interpreting theecho '*'
*
as a
special character.
Double quote characters are weaker. Inside double quotes, the shell performs
command substitutions of the form
(See Command Substitution.) The shell does not perform such substitutions when they appear inside apostrophes. In addition, the shell performs parameter substitutions of the form$(command) or `command`
when they are inside double quotes but not when they're inside apostrophes (see Parameter Substitution). As well, you can use the backslash to escape another character inside double quotes, but inside apostrophes, the shell ignores this special meaning. The shell treats internal field separator characters (that is, characters in the value of the$parameter
IFS
variable) literally inside quoted
arguments, whether they're quoted with double quotes or apostrophes. This means
that a quoted argument is considered a single entity, even if it contains
IFS
characters.
Quoting can override the special meanings of reserved words and aliases.
For example, in
the quotes around"select" program
select
tell the shell not to interpret
select
as a shell reserved word. Instead,
sh
does a normal command search for a command named
select
.
You must always quote the following characters if you want
sh
to interpret them literally:
The following characters need to be quoted in certain contexts if they are to be interpreted literally.| & ; < > ( ) $ ' " ` \ <space> <tab> <newline>
* ? [ # % = ~
0
through 9
. It
may have any number of digits. For example, the file descriptors
001
and 01
are identical to file descriptor
1
. Various operations can associate a file descriptor with a
particular file; the exec
command is
used to manipulate file descriptors directly.
Some file descriptors are set up at the time the shell starts up.
These are the standard input/output streams:
0
through
9
, using the following command line constructs.
<
fileuses file for input on the file descriptor number.
If you omit number, as in <
file,
the default is 0; this redirects the standard input.
>
fileuses file for output on the file descriptor number. If
you omit number, as in >
file, the default is
1; this redirects the standard output. The shell creates the file if it
doesn't already exist. The redirection fails if the file already exists
and noclobber
is set (see
set
).
>|
fileis similar to number>
file but if
file already exists, it overwrites the current contents of the
file.
<>
fileuses file for input and output with the file descriptor
number. This is most useful when the file is another terminal or
modem line. If you omit number, as in
<>
file, the default number is zero; this
redirects the standard input. Output written to the file overwrites the
current contents of the file (if any). The shell creates the file if it
doesn't already exist.
>>
fileis similar to number >
file except that
output is appended to the current contents of file (if
any).
<<[-]
namelets you specify input to a command from your terminal (or from the body of a shell script). This notation is known as a here document. The shell reads from the standard input and feeds that as input to file descriptor number until it finds a line that exactly matches the given name. If you omit number, the default is the standard input. For example, to process the command
the shell reads input from the terminal until you enter a line that consists of the wordcat <<abc >out
abc
. This input is passed as the
standard input to the cat
command, which then copies the text to the file
out
.
If any character of name is quoted or escaped,
sh
does not perform substitutions on the input;
otherwise, it performs variable and command substitutions, respecting the
usual quoting and escape conventions. If you put -
before
name, sh
deletes all leading tabs in the
here document.<&
wordmakes the input file descriptor number a duplicate of the file
descriptor identified by the expansion of word. If you omit
number, the default is the standard input (file descriptor 0).
After expansion, the word can be a digit representing a file
descriptor (0
-9
), or a minus sign
(-
). For example, <&4
makes the standard
input a duplicate of file descriptor 4. In this case, entering input on 4
has the same effect as entering input on the standard input.
-
, the shell closes the
file descriptor named by number.>&
wordmakes the output file descriptor number a duplicate of the file
descriptor identified by the expansion of word. If you omit
number, the default is the standard output (file descriptor 1).
After expansion, the word can be a digit representing a file
descriptor (0
-9
), or a minus sign
(-
). For example, >&2
makes the standard
output a duplicate of file descriptor 2 (the standard error). In this case,
writing output on the standard output has the same effect as writing
output on the standard error.
-
, the shell closes the file
descriptor named by number.<&-
closes input descriptor number. If you omit number, it closes the standard input.
>&-
closes output descriptor number. If you omit number, it closes the standard output.
exec
).
The order of redirection specifications is significant,
since an earlier redirection can affect a later one; however, these
specifications may be freely intermixed with other command arguments.
Since the shell takes care of the redirection, these constructs are not
passed to the command itself.
The shell performs the implicit redirections needed for pipelines before performing any explicit redirections.
*
, ?
and [
are called
glob characters or wild card characters.
If an unquoted argument contains one or more glob characters,
the shell processes the argument for file name generation.
The glob characters are part of glob patterns
which represent file and directory names. These patterns are similar
to regular expressions, but differ in syntax, since they are intended
to match file names and words (not arbitrary strings). The special
constructions that may appear in glob patterns are:
?
Matches exactly one character of a file name, except for the separator
character /
and a .
at the beginning of a file
name. ?
only matches an actual file name character and does
not match nonexistent characters at the end of the file name.
?
is analogous to the metacharacter .
in regular
expressions.
*
Matches zero or more characters in a file name, subject to the same
restrictions as ?
. *
is analogous to the regular
expression .*
. *x.c
is a valid pattern.
[
chars]
Defines a class of characters; the glob pattern matches
any single character in the class. A class may contain a range of
characters by writing the first character in the range, a dash
-
and the last character. For example, in the POSIX locale
[A-Za-z]
stands for all the uppercase and lowercase letters.
If you want a literal -
character in the class, put it as the
first or last character inside the brackets. If the first character
inside the brackets is an exclamation mark (!
), the pattern
matches any single character that is not in the class.
|
or &
.
If two patterns are separated by |
, only one of the two patterns
must match. If two patterns are separated by &
, both patterns
must match.
A sub-pattern begins with a ?
, *
, +
,
@
, or !
character followed by a
pattern-list enclosed in parentheses. Pattern-lists themselves can
contain sub-patterns. The following list describes valid sub-patterns.
?(
pattern-listMatches exactly zero or exactly one occurrence of the specified pattern-list.
*(
pattern-listMatches zero or more occurrences of the specified pattern-list.
+(
pattern-listMatches one or more occurrences of the specified pattern-list.
@(
pattern-listMatches exactly one occurrence of the specified pattern-list.
!(
pattern-listMatches any string that does not match the specified pattern-list.
[!a-f]*.c
matches all .c
files beginning with something other than
the letters a
through f
.
/???/?.?
matches all files under the root directory in a directory that has a
three letter name. The files must have a base name containing one
character followed by a .
and another single character.
*/*.[chyl]
matches all .c
, .h
, .y
, and
.l
files in a subdirectory of the current directory.
~mks/*.ksh
matches all shell scripts in the home directory of user mks
(see Directory Substitution
for the use of ~
).
test_?([fr]un|log)_file
matches files with the names test__file
,
test_fun_file
, test_run_file
, and
test_log_file
.
prog*([a-z]).c
matches the file prog.c
and any .c
file whose
name consists of prog
followed by any combination of
lowercase letters.
file+([0-9])
matches all files whose names consist of the word file
followed by one or more digits.
@(install|setup|config).bat
matches the file names install.bat
, setup.bat
,
and config.bat
.
*.!([bcd]at)
matches all files except those with .bat
,
.cat
, or .dat
extensions.
sh
leaves the argument
untouched. If the set
option
-f
or -o noglob
is in effect, the shell
does not perform file name generation.
By default, file name generation is case-insensitive. If you want file
name generation to be case-sensitive, set the environment variable
DUALCASE
to any value. Setting
DUALCASE
also makes pattern-matching in
case
structures case-sensitive.
_
). Subsequent characters in the name, if any, may be
uppercase or lowercase letters, underscores, and/or digits 0
through 9
. You can assign a value to a variable with
You may implicitly declare a variable as an array by using a subscript expression when assigning a value, as invariable=value
You can use a subscripted array variable anywhere that the shell allows an ordinary variable. See the section on Arithmetic Substitution for the syntax of an arithmetic expression. Also seevariable[arithmetic expression]=value
typeset
,
export
, and
readonly
for details about the
attributes of shell variables and how shell variables may be exported to child
processes.
For a list of variables that the shell either sets or understands,
see ENVIRONMENT VARIABLES.
cd
)umask
)trap
)set
and export
)set
)ENV
environment variable is not run. Changes made to
a subshell environment do not affect the shell environment.
Command substitutions, commands within parentheses (for example,
(
command), and commands to be run asynchronously (for
example, command&
) all run in subshell environments.
Each command in a pipeline command|
command runs in a
subshell environment.
Shell utilities also run in a separate environment which does not affect the
shell environment, except for certain built-in utilities (for example,
cd
and
umask
) which explicitly alter the
shell environment. The shell sets up a shell utility's environment to include
the following:
cd
)umask
)export
attributewhence
command to determine if a
specific command is a built-in on your system. The most common built-in commands
are listed here.
POSIX.2 recognizes a subset of these commands as special built-ins. Syntax errors in special built-in commands cause a non-interactive shell to exit with the exit status set by the command. The special built-in utilities are:: . [[ [ alias break cd command continue echo eval exec exit export false fc getopts jobs kill let print pwd read readonly return set shift test time times trap true type typeset umask unalias unset wait whence
As well as built-in commands, the shell has a set of predefined aliases:: . break continue eval exec exit export readonly return set shift trap typeset unset
Seefunctions hash hist history integer nohup r stop suspend
alias
for details.
The following simple script produces as much of the Fibonacci sequence as can be calculated using integers.# This is a shell archive. # It contains the one file "frag.ksh" # To extract contents, type # sh file # if [ -f frag.ksh ] then echo frag.ksh exists: will not overwrite else echo extracting frag.ksh sed 's/^X//' >frag.ksh <<_EOF_ X# This is frag.ksh X# Not very interesting, really. Xecho frag.ksh here! _EOF_ if [ "`sum frag.ksh|awk '{print $1}'`" != 52575 ] then echo frag.ksh damaged in transit fi fi
The following implements the# Print out Fibonacci sequence; start sequence # with first two positional parameters: # default 1 1 typeset -i x=${1:-1} y=${2:-1} z while [ x -gt 0 ] # until overflow do echo $x let z=y+x x=y y=z done
basename
utility as a shell
function.
# basename utility as shell function function basename { case $# in 1) ;; 2) eval set \${1%$2} ;; *) echo Usage: $0 pathname '[suffix]' return 1 ;; esac echo ${1##*/} return 0 }
_
(underscore) expands to the last argument of the previously executed
command. For every command that is executed as a child of the shell,
sh
sets this variable to the full path name of the
executable file and passes this value through the environment to that
child process. When processing the MAILPATH
variable, this variable holds the value of the corresponding mail
file.
CDPATH
contains a list of directories for the
cd
command to search. Directory
names are separated with colons on POSIX and UNIX systems.
CDPATH
works in a similar way to
the PATH
variable.
COLUMNS
contains the maximum width of the edit window in the KornShell
vi
or emacs
editing modes. It is
also used by several other commands to define the width of the terminal
output device.
DUALCASE
when set, makes pattern-matching in case
statements
and file name generation case-sensitive. By default,
DUALCASE
is not set and as a result,
pattern-matching in file name generation is case-insensitive.
EDITOR
enables the corresponding editing mode (see the
set
and
shedit
reference pages) when
using vi
, emacs
, or
gmacs
. If VISUAL
is also
set, its value takes precedence over
EDITOR
's.
ERRNO
contains the system error number of the most recently failed system call. The shell only sets this variable for errors which occur in the current environment. Assigning a value of 0 to this variable clears it.
ENV
contains a value on which sh
performs parameter
substitution and uses the result as the name of an initialization file.
This file is executed with the .
command. This
facility lets you define functions (see
Command Syntax), aliases (see
alias
) and other non-exported
items during shell initialization.
FCEDIT
contains the path name of the default editor for the
fc
command. If this variable is
not set, the default is the ed
command.
FIGNORE
contains a pattern which determines files that are to be ignored during file expansion. For example, suppose a directory contains:
To ignore all files with afile.c file.o file1.c file1.o file2.c file2.o
.o
extension, you do the following:
Note that since the first$ FIGNORE='*.o' $ ls file.c file.o file1.c file1.o file2.c file2.o $ ls * file.c file1.c file2.c
ls
does
no file name expansion, it still displays the .o
files;
however the second ls
command uses file name expansion and hence ignores those files.HASHBANG
enables or disables the #!
feature of the MKS KornShell.
If this variable is set, the feature is enabled; otherwise, it is disabled.
See the Command Execution section
for more details.
HISTCMD
is set by the MKS KornShell to contain the number of the current command in the history file.
HISTEDIT
contains the path name of the default editor for the
hist
command. If this variable is
not set, hist
uses the editor
indicated by the FCEDIT
environment variable.
Finally, if neither of these variables are set, it uses the
ed
command.
HISTFILE
contains the path name of a file to be used as the history file. When the shell starts, the value of this variable overrides the default history file. See FILES.
HISTSIZE
contains the maximum number of commands that the shell keeps in the history file. If this variable contains a valid number when the shell starts, it overrides the default of 127.
HOME
contains your home directory. This is the default directory for the
cd
command.
IFS
contains a series of characters to be used as internal field
separator characters. During word expansion (see
Word Expansion), the presence of any
of these characters within a word causes it to be split. In addition, the
shell uses these characters to separate values put into variables with
the read
command. Lastly, the
first character in the value of IFS
separates
the positional parameters in $*
expansion. By default,
IFS
contains space, tab and newline.
LINENO
contains the number of the line currently being executed by a shell script.
LINES
contains a numeric value that limits the number of output lines used
by the select
statement in printing its menu.
MAIL
contains the path name of a mailbox. If
MAILPATH
is not set, the MKS KornShell tells
you when new mail arrives in this file. The shell assumes that new mail
has arrived if the file's modify time changes.
MAILCHECK
contains the number of seconds of elapsed time that must pass before
checking for mail; if not set, the default value is 600 seconds. When
using the MAIL
or
MAILPATH
variables, the MKS KornShell checks
for mail before issuing a prompt.
MAILPATH
contains a list of mailbox files. This overrides the
MAIL
variable. The mailbox list is separated by
colons on POSIX and UNIX systems. If any name is followed by
?
message or %
message,
sh
displays the message if the corresponding file has
changed. sh
performs parameter and command
substitution on message and the variable
_
(temporarily) expands to the name of the
mailbox file. If no ?
message or
%
message is present, the default message is you
have mail in $_.
.
OLDPWD
contains the path name of the previous directory. The
cd
command sets this
variable.
PATH
contains a list of directories which constitute the search path for
executable commands. Directories in this list are separated with colons
on POSIX and UNIX systems. sh
searches each directory
in the order specified in the list until it finds a matching executable.
If you want the shell to search the current directory, put a null string
in the list of directories (for example, starting the list with a
colon/semicolon tells the shell to search the current directory
first).
PPID
contains the decimal value of the process ID of the parent of the shell.
PS1
contains the primary prompt string used when the shell is interactive.
The default value is a dollar sign followed by a space ($
).
The shell expands parameters before the prompt is printed. A single
exclamation mark (!
) in the prompt string is replaced by the
command number from the history list (see
fc
); for a real exclamation mark
in the prompt, use !!
.
PS2
contains the secondary prompt, used when completing the input of such
things as reserved word commands, quoted strings, and here
documents. The default value of this variable is a greater than
sign followed by a space (>
).
PS3
contains the prompt string used in connection with the
select
reserved word. The default value is a number
sign followed by a question mark and a space (#?
).
PS4
contains the prefix for traced commands with
set
-x
.
The default value is a plus sign followed by a space
(+
).
PWD
contains the path name of the current working directory. When the shell
starts, the current directory name is assigned to
PWD
unless the variable already has a
value.
RANDOM
expands to a random integer. Assigning a value to
RANDOM
sets a new seed for the random number
generator.
REPLY
contains the user input from the select
statement
(see Command Syntax). The
read
command also sets this
variable if no variable is specified.
SECONDS
contains elapsed time. The value of this variable grows by 1 for each
elapsed second of real time. Any value assigned to this variable sets the
SECONDS
counter to that value; initially the
shell sets the value to 0.
SHELL
contains the full path name of the current shell. It is not set by the shell, but is used by various other commands to invoke the shell.
TMOUT
contains the number of seconds before user input times out. If user input has not been received within this length of time, the shell terminates.
VISUAL
overrides the EDITOR
variable in setting
vi
, emacs
, or
gmacs
editing modes (see the
shedit
reference page).
sh_history
default history storage file
$HOME/.profile
profile for login shell
/etc/profile
system-wide profile for login shells
/tmp/sh*
Temporary files for here documents, command substitution,
history re-execution, etc. The default directory /tmp
can be
overridden by setting the shell variable TMPDIR
to the name of some other directory. See the
envvar
reference
page.
0
1
exit
or
return
commands. The exit status of
a pipeline is the exit status of the last command in the pipeline.
Most diagnostics are self explanatory. See the command reference pages for
diagnostics from built-in commands.
A redirection construct expanded to more than one path name.
You are attempting to redirect output into an existing file, but you
have turned on the noclobber
option (see
set
). If you really want to
redirect output into an existing file, use the construct
>|
filename, or turn off the option with
set +o noclobber
You attempted to redirect a file descriptor that was already being redirected in the same command. You can only redirect a file descriptor once.
The shell received a hangup signal. This signal typically arises when a communication line is disconnected (for example, when a phone connection is cut off). When the shell receives a hangup signal, it sends a hangup signal to all process groups, foreground and background, and then exits.
In a number with the form base#
number, the
value of the base was larger than 36 or less than 2. The only valid range
for bases is from 2 through 36.
A shell array was indexed with a subscript outside the defined bounds.
The shell received an illegal instruction signal. This typically occurs when a process tries to execute something that is not a valid machine instruction recognized by the hardware.
The subscript for an array was missing or invalid.
You attempted to use a non-alphanumeric Name.
The given Name is a read-only variable and cannot be removed or
changed (see
readonly
).
The shell is operating with
set
-u
and
you used an unset variable in a substitution. For more information, see
set
.
When a file descriptor is redirected, the shell remembers the old value by duplicating it to another file descriptor. Since the total number of file descriptors is limited by the system, the shell may run out of descriptors while it looks like your command is using far fewer than the maximum number allocated.
If the shell has been invoked as a restricted shell, certain things are
disallowed (for example, the cd
command, setting PATH
, and output
redirection).
sh
tried to create a temporary file to hold the
contents of a <<
word here document, but
the temporary file could not be created. This may indicate a lack of space
on the disk where temporary files are created.
select
and function
reserved
word commandsERRNO
, LINENO
,
PS3
, PWD
,
OLDPWD
, TMOUT
,
HISTEDIT
, and FIGNORE
shell variables++
and --
arithmetic operators[[
and
hist
built-in commands${
parameter:
offset:
len}
,
${
parameter:
offset}
,
${
parameter/
pat/
str}
,
${
parameter//
pat/
str}
,
${
parameter/#
pat/
str}
, and
${
parameter/%
pat/
str}
parameter expansions.?(
pattern-list, *(
pattern-list,
+(
pattern-list, @(
pattern-list,
and !(
pattern-list sub--patterns-r
are from
SVID and the KornShell.
The construct $[
arithmetic expression]
is an extension to the POSIX standard.
The maximum length of an executable file name, including subdirectories and extensions, depends upon the operating system.cannot execute: More data is available
_
), and cannot begin with a digit; however,
some applications require a shell variable to be set that does not follow these
naming conventions. To set such a variable, you can use the
env
command to insert the normally
invalid name directly into the environment block. For example, to run a program
that requires a variable named 386
to be set to yes
,
you can use the following command:
env '386=yes' program