|
|
coherent
sh Command sh
Command language interpreter
sshh [-cceeiikknnssttuuvvxx] _t_o_k_e_n ...
sh, also called the shell, is the default COHERENT command lan-
guage interpreter. Other command languages can be provided on a
per-user basis. The tutorial included in this manual describes
the shell in detail.
The shell reads commands from the terminal or from a file and in-
terprets them. A command may be either a program or a text file
containing other commands. Shell constructs provide control flow
logic. Commands can contain patterns, which the shell expands
into file names. The shell can redirect input and output.
***** Commands *****
A command consists of a command name and optional command ar-
guments, called tokens. A token is a string of graphic charac-
ters separated by spaces or tabs. Normally, the first token in a
command is the command name.
Commands are combined with the pipe operator `|' to form
pipelines. In the pipeline
a | b
the shell passes the standard output of a to the standard input
of b. The shell runs each command in the pipeline as a separate
process, and waits for the last command to finish before con-
tinuing.
Commands and pipelines can be joined into a sequence by using the
tokens `;', `&', `&&', and `||', in addition to newlines. The
shell executes commands or pipelines separated by newlines or by
`;' sequentially. For example,
a | b ; c | d
first executes the pipeline a | b and then executes the pipeline
c | d. The shell executes any command followed by `&'
asynchronously as a background (or detached) process and prints
its process id. The shell executes the command following the
token `&&' only if the preceding command returns a zero exit
status, signifying success. Similarly, it executes the command
following `||' only if the preceding command returns a nonzero
exit status, signifying failure. Newline, `;' and `&' bind less
tightly than `&&' and `||'; the shell parses command lines from
left to right if separators bind equally.
COHERENT Lexicon Page 1
sh Command sh
***** I/O Redirection *****
The standard input, standard output, and standard error streams
are normally connected to the terminal. A pipeline attaches the
output of one command to the input of another command. In addi-
tion, the operators `>', `>>', `<', and `<<' redirect the stan-
dard output and the standard input.
Output redirection sends standard output to file rather than to
the terminal:
>file
creates file if it does not exist, and destroys its previous con-
tents if it does exist. The operator
>>file
appends standard output to an existing file, or creates file if
it does not exist.
In input redirection,
<file
accepts standard input from file rather than from the terminal.
The input redirection operator
<<token
accepts standard input from the shell input until the next line
containing only token in the shell input. The shell input be-
tween tokens is called a here document. The shell will perform
parameter substitution on the here document unless the leading
token is quoted; parameter substitution and quoting are described
below.
The standard input and output may also be redirected to duplicate
other file descriptors. The operator `<&_n' duplicates the stan-
dard input from file descriptor n, and `>&_n' duplicates the stan-
dard output. The operators `<&-' and `>&-' close the standard
input and output.
Other descriptors may be redirected by preceding the `<' or `>'
with the digit of the descriptor to be redirected. For example,
COHERENT Lexicon Page 2
sh Command sh
2>&1
redirects file descriptor 2 (the standard error) to file descrip-
tor 1 (the standard output). The system call dup performs file
descriptor duplication.
Each command executed as a foreground process inherits the file
descriptors and signal traps (described below) of the invoking
shell, modified by any specified redirection. Background proces-
ses take input from the null device /dev/null (unless redirected)
and ignore interrupt and quit signals.
***** File Name Patterns *****
The shell interprets an input token containing any of the special
characters `?', `*', or `[' as a file name pattern. The question
mark `?' matches any single character except newline. The as-
terisk `*' matches a string of non-newline characters of any
length (including zero). Square brackets `[ ]' enclose alter-
natives to match a single character; as in ed, ranges of charac-
ters can be separated by `-'. The slash `/' and leading period
`.' must be matched explicitly in a pattern. The shell generates
an alphabetized list of file names matching the pattern to
replace the token. It passes the token unchanged if it finds no
match.
In addition, the characters `\', `"', and `'' remove the special
meaning of other characters. The backslash `\' quotes the
following character. The shell ignores a backslash immediately
followed by a newline, called a concealed newline. A pair of
apostrophes ' ' prevents interpretation of any enclosed special
characters. A pair of quotation marks " " has the same effect,
except that parameter substitution and command output substitu-
tion (described below) occur within quotation marks.
***** Scripts *****
Shell commands can be stored in a file, or script. The command
sh _s_c_r_i_p_t [ _p_a_r_a_m_e_t_e_r ... ]
executes the commands in script with a new subshell sh. Each
parameter is a value for a positional parameter, as described
below. If script has been made executable with the chmod com-
mand, the sh may be omitted.
Formal parameters of the form `$_n', where n ranges from zero
through nine, represent positional parameters in a script. The
parameter `$0' gives the name of the script. If no corresponding
actual parameter is given on the command line, the shell sub-
stitutes the null string for the positional parameter. The shell
COHERENT Lexicon Page 3
sh Command sh
substitutes the actual values of all positional parameters for
the reference `$*'.
Commands in a script can also be executed with the . (dot) com-
mand. It resembles the sh command, but the current shell ex-
ecutes the script commands without creating a new subshell or a
new environment; positional parameters are not allowed.
***** Variables *****
Shell variables are names which may be assigned string values on
a command line, in the form
_n_a_m_e=_v_a_l_u_e
The name must begin with a letter, and may contain letters,
digits, and underscores `_'. In shell input, `$_n_a_m_e' or
`${_n_a_m_e}' represents the value of the variable. If an assignment
precedes a command on the same command line, the effect of the
assignment is local to the command; otherwise, the effect is per-
manent. For example,
kp=one testproc
assigns variable kp the value one only for the execution of the
script testproc.
The shell sets the following variables:
# The number of actual positional parameters given to the cur-
rent command.
@ The list of positional parameters ``$1 $2 ...''.
* The list of positional parameters ``$1'' ``$2'' ... (the same
as `$@' unless quoted).
- Options set in the invocation of the shell or by the set com-
mand.
? The exit status returned by the last command.
! The process number of the last command invoked with `&'.
$ The process number of the current shell.
The shell also references the following variables:
HHOOMMEE Initial working directory; usually specified in the
password file /etc/passwd.
COHERENT Lexicon Page 4
sh Command sh
IIFFSS Delimiters for tokens; usually space, tab and newline.
LLAASSTTEERRRROORR
Name of last command returning nonzero exit status.
MMAAIILL Checked at the end of each command. If file specified in
this variable is new since last command, the shell prints
``You have mail.'' on the user's terminal.
PPAATTHH Colon-separated list of directories searched for com-
mands.
PPSS11 First prompt string, usually `$'.
PPSS22 Second prompt string, usually `>'. Used when the shell
expects more input, such as when an open quote has been
typed but a close quote has not been typed, or within a
shell construct.
The special forms `${_n_a_m_e_c_t_o_k_e_n}', where c is one of the charac-
ters `-', `=', `+', or `?', perform conditional parameter sub-
stitution. The shell replaces the form `${_n_a_m_e-_t_o_k_e_n}' by the
value of name if it is set and by token otherwise. The `=' form
has the same effect, but also sets the value of name to token if
it was not set previously. The shell replaces the `+' form by
token if the given name is set. The shell replaces the `?' form
by the value of name if set, and otherwise prints token and exits
from the shell.
***** Command Output Substitution *****
The shell can use the output of a command as shell input (as com-
mand arguments, for example) by enclosing the command in grave
characters ` `. To list directories given in a file dirs, use
the command
ls -l `cat dirs`
***** Constructs *****
The shell provides control over execution of commands by the
bbrreeaakk, ccaassee, ccoonnttiinnuuee, ffoorr, iiff, uunnttiill, and wwhhiillee constructs. The
shell recognizes each reserved word only if it occurs unquoted as
the first token of a command. This implies that a separator must
precede each reserved word in the following constructs. For ex-
ample, newline or `;' must precede do in the for construct.
bbrreeaakk [_n]
Exit from for, until, or while. If n is given, exit from n
levels.
ccaassee _t_o_k_e_n iinn [ _p_a_t_t_e_r_n [ | _p_a_t_t_e_r_n ] ...) _s_e_q_u_e_n_c_e;; ] ... eessaacc
Check the token against each pattern, and execute the se-
COHERENT Lexicon Page 5
sh Command sh
quence associated with the first matching pattern.
ccoonnttiinnuuee [_n]
Branch to the end of the nth enclosing for, until, or while
construct.
ffoorr _n_a_m_e [ iinn _t_o_k_e_n ... ] ddoo _s_e_q_u_e_n_c_e ddoonnee
Execute sequence once for each member of the specified token
list. On each iteration, the name takes the value of the
next token in the list. If the in clause is omitted, $@ is
assumed. For example, to list all files ending with .c:
for i in *.c
do cat $i
done
iiff _s_e_q_1 tthheenn _s_e_q_2 [ eelliiff _s_e_q_3 tthheenn _s_e_q_4 ] ... [ eellssee _s_e_q_5 ] ffii
Execute seq1. If the exit status is zero, execute seq2. If
not, execute the optional seq3 if given. If its exit status
is zero, execute seq4 and so on. If the exit status of each
tested sequence is nonzero, execute seq5.
uunnttiill _s_e_q_u_e_n_c_e_1 [ ddoo _s_e_q_u_e_n_c_e_2 ] ddoonnee
Execute sequence2 until the execution of sequence1 results
in an exit status of zero.
wwhhiillee _s_e_q_u_e_n_c_e_1 [ ddoo _s_e_q_u_e_n_c_e_2 ] ddoonnee
Execute sequence2 as long as the execution of sequence1
results in an exit status of zero.
(_s_e_q_u_e_n_c_e)
Execute the sequence within a subshell. This allows the se-
quence to change the current directory, for example, and not
affect the enclosing environment.
{_s_e_q_u_e_n_c_e}
Braces simply enclose a sequence.
***** Special Commands *****
The shell usually executes commands by a fork system call, which
creates another process. However, the shell executes the com-
mands in this section either directly or with an exec system
call. The Lexicon describes fork and exec.
. _s_c_r_i_p_t
Read and execute commands from script. Positional
parameters are not allowed. The shell searches PATH to find
the given script.
: [_t_o_k_e_n ...]
A colon `:' indicates a ``partial comment''. The shell nor-
mally ignores all commands on a line that begins with a
colon, except for redirection and such symbols as $, {, ?,
COHERENT Lexicon Page 6
sh Command sh
etc.
# A complete comment: if # is the first character on a line,
the shell ignores all text that follows on that line.
ccdd [_d_i_r]
Change the working directory to dir. If no argument is
given, change to the home directory.
eevvaall [_t_o_k_e_n ...]
Evaluate each token and treat the result as shell input.
eexxeecc [_c_o_m_m_a_n_d]
Execute command directly rather than performing fork. This
terminates the current shell.
eexxiitt [_s_t_a_t_u_s]
Set the exit status to status, if given; otherwise, the pre-
vious status is unchanged. If the shell is not interactive,
terminate it.
eexxppoorrtt [_n_a_m_e ...]
The shell executes each command in an environment, which is
essentially a set of shell variable names and corresponding
string values. The shell inherits an environment when in-
voked, and normally it passes the same environment to each
command it invokes. export specifies that the shell should
pass the modified value of each given name to the environ-
ment of subsequent commands. When no name is given, the
shell prints the name and value of each variable marked for
export.
rreeaadd _n_a_m_e ...
Read a line from the standard input and assign each token of
the input to the corresponding shell variable name. If the
input contains fewer tokens than the name list, assign the
null string to extra variables. If the input contains more
tokens, assign the last name the remainder of the input.
rreeaaddoonnllyy [_n_a_m_e ...]
Mark each shell variable name as a read only variable. Sub-
sequent assignments to read only variables will not be per-
mitted. With no arguments, print the name and value of each
read only variable.
sseett [-cceeiikknnssttuuvvxx [_n_a_m_e ...] ]
Set listed flag. If name list is provided, set shell
variables name to values of positional parameters beginning
with $1.
sshhiifftt
Rename positional parameter 1 to current value of $2, and so
on.
COHERENT Lexicon Page 7
sh Command sh
ttiimmeess
Print the total user and system times for all executed
processes.
ttrraapp [_c_o_m_m_a_n_d] [_n ...]
Execute command if the shell receives signal n. If command
is omitted, reset traps to original values. To ignore a
signal, pass null string as command. With n zero, execute
command when the shell exits. With no arguments, print the
current trap settings.
uummaasskk [_n_n_n]
Set user file creation mask to nnn. If no argument is
given, print the current file creation mask.
wwaaiitt [_p_i_d]
Hold execution of further commands until process pid ter-
minates. If pid is omitted, wait for all child processes.
If no children are active, this command finishes im-
mediately.
***** Options *****
-cc _s_t_r_i_n_g
Read shell commands from string.
-ee Exit on any error (command not found or command returning
nonzero status) if the shell is not interactive.
-ii The shell is interactive, even if the terminal is not at-
tached to it; print prompt strings. For a shell reading a
script, ignore the signals SIGTERM and SIGINT.
-kk Place all keyword arguments into the environment. Normally,
the shell places only assignments to variables preceding the
command into the environment.
-nn Read commands but do not execute them.
-ss Read commands from the standard input and write shell output
to the standard error.
-tt Read and execute one command rather than the entire file.
-uu If the actual value of a shell variable is blank, report an
error rather than substituting the null string.
-vv Print each line as it is read.
-xx Print each command and its arguments as it is executed.
- Cancel the -x and -v options.
COHERENT Lexicon Page 8
sh Command sh
If the first character of argument 0 is `-', the shell reads and
executes the scripts /etc/profile and $HOME/.profile before
reading the standard input. /etc/profile is a convenient place
for initializing system-wide variables, such as TIMEZONE.
***** Examples *****
The first example is a shell script that moves to the next al-
phabetical sibling directory.
# DEF_NAME is a command that defines the current directory name.
DEF_NAME="basename `pwd`"
# CUR_DIR current directory name.
CUR_DIR=`$DEF_NAME`
# If current directory is root exit, else
# go the to parent directory.
if [ $CUR_DIR = '/' ]; then
echo This is root directory.
exit
else
cd ..
fi
# DIR_NUM is the alphabetical number of the current directory
# in the directory list of the parent directory.
DIR_NUM=`lc -d1 | sed -n -e "/ $CUR_DIR/="`
# NEXT is the number of the next alphabetical directory.
NEXT=`expr $DIR_NUM + 1`
# If next directory exists then come to this directory,
# else stay in parent directory.
if [ $NEXT -le `lc -d1 | wc -l` ]; then
cd `lc -d1 | sed -n -e $NEXT\p`
fi
The second example is a script that logs UUCP information to a
file. Usage is uuuuiinnffoo _o_u_t_f_i_l_e, where outfile is the file to hold
the logged information.
COHERENT Lexicon Page 9
sh Command sh
OUTFILE=$1
> $OUTFILE
echo "Descriptive text for top of file, ended by Ctrl-D:"
echo "UUCP and Com Port Information." >> $OUTFILE
cat >> $OUTFILE
echo "===============================================" >> $OUTFILE
echo "/usr/lib/uucp" >> $OUTFILE
(
cd /usr/lib/uucp
echo "==============================================="
ls -l L.sys L-devices Permissions
echo "==============================================="
echo "L.sys"
echo "==============================================="
cat L.sys
echo "==============================================="
echo "L-devices"
echo "==============================================="
cat L-devices
echo "==============================================="
echo "Permissions"
echo "==============================================="
cat Permissions
echo "==============================================="
) >> $OUTFILE
echo "/etc/ttys" >> $OUTFILE
echo "===============================================" >> $OUTFILE
ls -l /etc/ttys >> $OUTFILE
echo "===============================================" >> $OUTFILE
cat /etc/ttys >> $OUTFILE
echo "===============================================" >> $OUTFILE
echo "/dev/com*" >> $OUTFILE
echo "===============================================" >> $OUTFILE
ls -l /dev/com* >> $OUTFILE
echo "===============================================" >> $OUTFILE
echo "End of file." >> $OUTFILE
echo "$OUTFILE written."
echo "Remove confidential passwords & phone numbers from $OUTFILE."
***** Files *****
/eettcc/pprrooffiillee -- System-wide initial commands
$HHOOMMEE/.pprrooffiillee -- User-specific initial commands
/ddeevv/nnuullll -- For background input
COHERENT Lexicon Page 10
sh Command sh
/ttmmpp/sshh* -- Temporaries
***** See Also *****
commands, dup(), environ, exec, fork(), login, newgrp, signal(),
test
_I_n_t_r_o_d_u_c_t_i_o_n _t_o _s_h, _t_h_e _B_o_u_r_n_e _S_h_e_l_l, tutorial
***** Diagnostics *****
The shell notes on the standard error syntax errors in commands
and commands which it cannot find. Syntax errors cause a nonin-
teractive shell to exit. It gives error messages if I/O redirec-
tion is incorrect. The shell returns the exit status of the last
command executed or the status specified by an exit command.
COHERENT Lexicon Page 11
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.