SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
NAME
sh, rsh - Shell, the standard/restricted command programming
language
USAGE
sh [ args ]
rsh [ args ]
DESCRIPTION
Sh is a command programming language that executes commands
read from a terminal or a file.
Rsh is a restricted version of the standard command inter-
preter sh. It is used to set up log-in names and execution
environments whose capabilities are more controlled than
those of the standard Shell. Rsh operates in much the same
manner as sh, except that you cannot do the following when
using the restricted Shell: 1) change directories (i.e.,
execute a cd (1) command), 2) set the value of the $PATH
variable, 3) specify path or command names containing a
slash (/), or 4) redirect output (> and >>). These restric-
tions are enforced after your .profile is interpreted.
When a command to be executed is found to be a Shell pro-
cedure, rsh invokes sh to execute it. Rsh assumes that you
do not have write and execute permissions in the same direc-
tory. The net effect of these rules is that the writer of
the .profile has complete control over your actions, by per-
forming guaranteed set-up actions and leaving you in an
appropriate directory (probably not the log-in directory).
The system administrator often sets up a directory of com-
mands (i.e., /usr/rbin) that rsh can safely invoke.
DEFINITIONS
A blank is a tab or a space. A name is a sequence of
letters, digits, or underscores beginning with a letter or
underscore. A parameter is a name, a digit, or any of the
following characters: *, @, #, ?, -, $, and !.
A simple-command is a sequence of nonblank words separated
by blanks. The first word specifies the name of the command
to be executed. Usually, the remaining words are passed as
arguments to the invoked command (exceptions are noted under
COMMANDS). The command name is passed as argument 0. Refer
to exec (2) for additional information about this. The
value of a simple-command is its exit status if it ter-
minates normally, or (octal) 200+status if it terminates
abnormally. See signal (2) for a list of status values.
A pipeline is a sequence of one or more commands separated
by a pipe character (|) or, for historical compatibility, a
Printed 6/10/85 SH-1
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
caret (^). The standard output of each command but the last
is connected by a pipe (2) to the standard input of the next
command. Each command is run as a separate process; the
Shell waits for the last command to terminate. The exit
status of a pipeline is the exit status of the last command.
A list is a sequence of one or more pipelines separated by
;, &, &&, or ||, and optionally terminated by ; or &. Of
these four symbols, ; and & have equal precedence, which is
lower than that of && and ||. The symbols && and || also
have equal precedence. A semicolon (;) causes sequential
execution of the preceding pipeline; an ampersand (&) causes
asynchronous execution of the preceding pipeline (i.e., the
Shell does not wait for that pipeline to finish). The sym-
bol && (||) causes the list following it to be executed only
if the preceding pipeline returns a zero (non-zero) exit
status. An arbitrary number of new-lines may appear in a
list, instead of semicolons, to delimit commands.
COMMANDS
The commands used by sh are either simple commands or one of
the following. Unless otherwise stated, the value returned
by a command is that of the last simple-command executed in
the command.
The standard output from a command enclosed in a pair of
grave accents (``) may be used as part or all of a word;
trailing new-lines are removed.
for name in word ... do list done
Each time a for command is executed, set name to the
next word taken from the in word list. If in word ...
is omitted, then the for command executes the do list
once for each positional parameter set. Execution ends
when there are no more words in the list.
case word in pattern | pattern ... ) list ;; ... esac
Execute the list associated with the first pattern that
matches word. The form of the patterns is the same as
that used for filename generation (see FILENAME GENERA-
TION) except that a slash, a leading period, or a
period immediately following a slash need not be
matched explicitly.
if list then list elif list then list ... else list fi
Execute the list following if. If it returns a zero
exit status, then execute the list following the first
then. Otherwise, execute the list following elif and,
if its value is zero, execute the list following the
next then. Failing that, execute the else list. If no
else list or then list is executed, then the if command
returns a zero exit status.
SH-2 Printed 6/10/85
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
while list do list done
Repeatedly execute the while list and, if the exit
status of the last command in the list is zero, execute
the do list; otherwise, terminate the loop. If no com-
mands in the do list are executed, then the while com-
mand returns a zero exit status; until may be used in
place of while to negate the loop termination test.
(list)
Execute list in a sub-Shell.
{ list;}
Simply execute list. Note the space after the left
brace. This is required.
name () { list;}
Define a function referenced by name. The body of the
function is the list of commands between { and }. Note
the space after the left brace. This is required.
Execution of functions is described below (see EXECU-
TION).
The following words are only recognized as the first word of
a command and when they are not quoted:
if then else elif fi case esac for while until do done { }
COMMENTS
In most cases, a word beginning with a pound sign (#) causes
that word and all the following characters up to a newline
to be ignored. This is not true when the pound sign is used
in a Shell script to specify which Shell the script should
run in, e.g., #! /bin/sh or #! /com/sh.
PARAMETERS
The following parameters are automatically set by the Shell:
# The number of positional parameters in decimal.
- Flags supplied to the Shell on invocation or by
the set command.
? The decimal value returned by the last
synchronously-executed command.
$ The process number of this Shell.
! The process number of the last background command
invoked.
Printed 6/10/85 SH-3
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
The following parameters are used by the Shell:
HOME The default argument (home directory)
for the cd command.
PATH The search path for commands (see EXECU-
TION below). You may not change PATH if
executing under rsh.
CDPATH The search path for the cd command.
MAIL 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.
MAILCHECK This parameter specifies how often (in
seconds) the Shell checks for the
arrival of mail in the files specified
by the MAILPATH or MAIL parameters. The
default value is 600 seconds (10
minutes). If set to 0, the Shell checks
before each prompt.
MAILPATH A list of filenames separated by a colon
(:). If this parameter is set, the
Shell informs you of the arrival of mail
in any of the specified files. Each
filename can be followed by a percent
sign (%) and a message that is printed
when the modification time changes. The
default message is you have mail.
PS1 Primary prompt string, by default a
pound sign (#).
PS2 Secondary prompt string, by default a
greater-than sign (>).
IFS Internal field separators, normally
space, tab, and newline.
SHELL When the Shell is invoked, it scans the
environment for this name (see ENVIRON-
MENT below). If it is found and there
is an ``r'' in the filename part of its
value, the Shell becomes a restricted
Shell.
The Shell gives default values to PATH, PS1, PS2, MAILCHECK,
and IFS. HOME and MAIL are set by login (1).
SH-4 Printed 6/10/85
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
PARAMETER SUBSTITUTION
The dollar sign character ($) introduces substitutable
parameters. There are two types of parameters, positional
and keyword. If parameter is a digit, it is a positional
parameter. Positional parameters may be assigned values by
set. Keyword parameters (also known as variables) may be
assigned values by writing:
name=value
name=value
...
Pattern-matching is not performed on value. There cannot be
a function and a variable with the same name.
${parameter}
Substitute the value, if any, of the parameter. The
braces are required only when parameter is followed by
a letter, digit, or underscore that is not to be inter-
preted as part of its name. If parameter is an aster-
isk (*) or an at sign (@), all the positional parame-
ters, starting with $1, are substituted (separated by
spaces). Parameter $0 is set from argument zero when
the Shell is invoked.
${parameter:-word}
If parameter is set and is non-null, substitute its
value; otherwise, substitute word.
${parameter:=word}
If parameter is not set or is null, set it to word;
substitute the value of the parameter. Positional
parameters may not be assigned to in this way.
${parameter:?word}
If parameter is set and is non-null, substitute its
value; otherwise, print word and exit from the Shell.
If word is omitted, print the message ``parameter null
or not set''.
${parameter:+word}
If parameter is set and is non-null, substitute word;
otherwise, substitute nothing.
In the above, word is not evaluated unless it is to be used
as the substituted string, so that, in the following exam-
ple, pwd is executed only if d is not set or is null:
echo ${d:-`pwd`}
If the colon (:) is omitted from the above expressions, the
Shell only checks to see whether parameter is set or not.
BLANK INTERPRETATION
After parameter and command substitution, the results of
Printed 6/10/85 SH-5
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
substitution are scanned for internal field separator char-
acters (those found in IFS) and split into distinct argu-
ments where such characters are found. Explicit null argu-
ments (enclosed in double or single quotes) are retained.
Implicit null arguments (those resulting from parameters
that have no values) are removed.
FILENAME GENERATION
Following substitution, each command word is scanned for
asterisks, question marks, and periods. If one of these
characters appears, the word is regarded as a pattern. The
word is replaced with alphabetically sorted filenames that
match the pattern. If no filename is found that matches the
pattern, the word is left unchanged. A period at the start
of a filename or immediately following a slash (/), as well
as the slash character itself, must be matched explicitly.
The characters and their meanings are:
* Matches any string, including the null string.
? Matches any single character.
[...]
Matches any one of the enclosed characters. A
pair of characters separated by a dash (-) matches
any character lexically between the pair,
inclusive. If the first character following the
opening bracket is an exclamation point (!), any
character not enclosed is matched.
QUOTING
The following characters have a special meaning to the Shell
and cause termination of a word unless quoted:
; & ( ) | ^ < > new-line space tab
A character may be quoted (i.e., made to stand for itself)
by preceding it with a backslash (\). The pair \newline is
ignored. All characters enclosed between a pair of single
quote marks, except a single quote, are quoted. Inside dou-
ble quote marks, parameter and command substitution occurs
and a backslash quotes the following characters: \, , ", and
$. A ``$*'' is equivalent to ``$1 $2 ...", while ``$@'' is
equivalent to ``$1'' ``$2'' ....
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, the
secondary prompt (i.e., the value of PS2) is issued.
INPUT/OUTPUT
Before a command is executed, its input and output may be
redirected using a special notation interpreted by the
SH-6 Printed 6/10/85
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
Shell. The following may appear anywhere in a simple com-
mand, or may precede or follow a command and are not passed
on to the invoked command; substitution occurs before word
or digit is used:
<word Use file word as standard input (file descrip-
tor 0).
>word Use file word as standard output (file
descriptor 1). If the file does not exist it
is created; otherwise, it is truncated to zero
length.
>>word 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.
<<-word Read the Shell input up to a line that is the
same as word, or to an end-of-file. The
resulting document becomes the standard input.
If any character of word is quoted, no
interpretation is placed upon the characters
of the document. Otherwise, parameter and
command substitution occurs; (unescaped) \new-
line is ignored; and \ must be used to quote a
backslash, a dollar sign, a grave accent, and
the first character of word. If a dash (-) is
appended to <<, all leading tabs are stripped
from word and from the document.
<&digit Use the file associated with file descriptor
digit as standard input. Do the same for the
standard output, using >&digit.
<&- Close the standard input. Do the same for the
standard output, using >&-.
If any of the above is preceded by a digit, the file
descriptor that will be associated with the file is that
specified by the digit (instead of the default 0 or 1). For
example:
... 2>&1
associates file descriptor 2 with the file currently associ-
ated with file descriptor 1.
The order in which redirections are specified is signifi-
cant. The Shell evaluates redirections left-to-right. For
example:
... 1>xxx 2>&1
first associates file descriptor 1 with file xxx. It asso-
ciates file descriptor 2 with the file associated with file
descriptor 1 (i.e., xxx). If the order of redirections is
Printed 6/10/85 SH-7
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
reversed, file descriptor 2 is associated with the terminal
(assuming file descriptor 1 had been associated with it) and
file descriptor 1 is associated with file xxx.
If a command is followed by & the default standard input for
the command is the empty file /dev/null. Otherwise, the
environment for executing a command contains the file
descriptors of the invoking Shell as modified by
input/output specifications.
Redirection of output is not allowed in the restricted
Shell.
ENVIRONMENT
The environment is a list of name-value pairs that is passed
to an executed program in the same way as a normal argument
list. Refer to environ (5) for more general information.
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. If you modify the value of any of these parameters
or create new parameters, none of these affects the environ-
ment unless the export command is used to bind the Shell's
parameter to the environment (refer to set -a). You may
remove a parameter from the environment with the unset com-
mand. The environment seen by any executed command is thus
composed of any unmodified name-value pairs originally
inherited by the Shell, minus any pairs removed by unset,
plus any modifications or additions, all of which must be
noted in export commands.
You can augment the environment for any simple command by
prefixing it with one or more assignments to parameters.
Thus,
TERM=450 cmd and
(export TERM; TERM=450; cmd)
are equivalent (as far as the execution of cmd is con-
cerned).
If the -k flag is set, all keyword arguments are placed in
the environment, even if they occur after the command name.
SIGNALS
The INTERRUPT and QUIT signals for an invoked command are
ignored if the command is followed by an ampersand (&).
Otherwise, signals have the values inherited by the Shell
from its parent, with the exception of signal 11. (Refer to
the trap command below.)
SH-8 Printed 6/10/85
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
EXECUTION
Each time a command is executed, the Shell carries out the
above substitutions. If the command name matches one of the
special commands listed below, it is executed in the Shell
process. If the command name does not match a special com-
mand, but matches the name of a defined function, the func-
tion is executed in the Shell process. Note how this
differs from the execution of Shell procedures. The posi-
tional parameters $1, $2, ... are set to the arguments of
the function. If the command name does not match a special
command or the name of a defined function, a new process is
created and an attempt is made to execute the command via
exec (2).
The Shell parameter PATH defines the search path for the
directory containing the command. Alternative directory
names are separated by a colon (:). The default path is
:/bin:/usr/bin (specifying the current directory, /bin, and
/usr/bin, in that order). Note that the current directory
is specified by a null pathname, which can appear immedi-
ately after the equal sign or between the colon delimiters
anywhere else in the path list. If the command name con-
tains a backslash, the search path is not used. Such com-
mands are not executed by the restricted Shell. Otherwise,
each directory in the path is searched for an executable
file. If the file has execute permission but is not an
a.out file, it is assumed to be a file containing Shell com-
mands. A sub-Shell is spawned to read it. A parenthesized
command is also executed in a sub-Shell.
The location in the search path where a command was found is
remembered by the Shell, to help avoid unnecessary invoca-
tions of exec later. If the command was found in a relative
directory, its location must be redetermined whenever the
current directory changes. The Shell forgets all remembered
locations whenever the PATH variable is changed or the hash
-r command is executed (see below).
SPECIAL COMMANDS
Input/output redirection is now permitted for these com-
mands. File descriptor 1 is the default output location.
: Do nothing but return an exit code of zero.
. file
Read and execute commands from file and return. Use
the search path specified by PATH to find the directory
containing file.
break [ n ]
Exit from the enclosing for or while loop, if any. If
n is specified, break n levels.
Printed 6/10/85 SH-9
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
continue [ n ]
Resume the next iteration of the enclosing for or while
loop. If n is specified, resume at the nth enclosing
loop.
cd [ arg ]
Change the current directory to arg. The Shell parame-
ter HOME is the default arg. The Shell parameter
CDPATH defines the search path for the directory con-
taining arg. Alternative directory names are separated
by a colon (:). The default path is <null> (specifying
the current directory). The current directory is
specified by a null pathname, which can appear immedi-
ately after the equal sign or between the colon delim-
iters anywhere else in the path list. If arg begins
with a slash (/), the search path is not used. Other-
wise, each directory in the path is searched for arg.
The cd command may not be executed by rsh.
If cd locates a directory via CDPATH, it typically
reports the new pathname. If, however, the path hap-
pens to include a softlink, this can be somewhat
misleading. For example, if CDPATH = robert, and a
user with the log-in name joe does a cd joe from his
current directory, the following occurs: 1) cd looks
for a joe directory in the present working directory.
It will not find it, so it 2) looks for a joe directory
in the //robert directory. If one exists, and is a
softlink (i.e., //robert/joe points off to //ho/joe),
the cd command states that it has changed directories
to //robert/joe instead of //ho/joe.
echo [ arg ... ]
Echo arguments. Refer to echo (1) for usage and
description.
eval [ arg ... ]
Read the arguments as input to the Shell and execute
the resulting command(s).
exec [ arg ... ]
Execute the command specified by the arguments in place
of this Shell without creating a new process.
Input/output arguments may appear and, if no other
arguments are given, cause the Shell input/output to be
modified.
exit [ n ]
Exit the Shell with the exit status specified by n. If
n is omitted, the exit status is that of the last com-
mand executed (an end-of-file will also cause the Shell
to exit.)
SH-10 Printed 6/10/85
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
export [ name ... ]
Mark the given names for automatic export to the
environment of subsequently-executed commands. If no
arguments are given, print a list of all names exported
in this Shell. Function names may not be exported.
hash [ -r ] [ name ... ]
For each name, determine and remember the location in
the search path of the command specified by name. If
-r is also used, the Shell forgets all remembered loca-
tions. If no arguments are given, information about
remembered commands is presented. Hits is the number
of times a command has been invoked by the Shell pro-
cess. Cost is a measure of the work required to locate
a command in the search path. There are certain situa-
tions which require that the stored location of a com-
mand be recalculated. Commands for which this is done
are indicated by an asterisk (*) adjacent to the hits
information. Cost is incremented when the recalcula-
tion is done.
newgrp [ arg ... ]
Perform the same function as exec newgrp arg ....
pwd Print the current working directory. See pwd (1) for
usage and description.
read [ name ... ]
Read one line from the standard input and assign the
first word to the first name, the second word to the
second name, etc., with leftover words assigned to the
last name. The return code is 0 unless an end-of-file
is encountered.
readonly [ name ... ]
Mark the given names as readonly and disallow any
changes to the values of the these names by subsequent
assignment. If no arguments are given, print a list of
all readonly names.
return [ n ]
Exit the function with the return value specified by n.
If n is omitted, the return status is that of the last
command executed.
set [ - -aefhkntuvx [ arg ... ] ]
-a Mark variables modified or created for export.
-e Exit immediately if a command exits with a non-
zero exit status.
-f Disable the filename generation.
-h Locate and remember function commands as functions
are defined (these are normally located when the
Printed 6/10/85 SH-11
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
function is executed).
-k Place all keyword arguments in the environment for
a command, not just those that precede the command
name.
-n Read commands but do not execute them.
-t Exit after reading and executing one command.
-u Treat unset variables as an error when substitut-
ing.
-v Print Shell input lines as they are read.
-x Print commands and their arguments as they are
executed.
- - Do not change any of the flags. This is useful in
setting $1 to -.
Using + rather than - causes these flags to be turned off.
These flags may also be used when invoking the Shell. The
current set of flags may be found in $-. The remaining
arguments are positional parameters assigned, in order, to
$1, $2, .... If no arguments are given the values of all
names are printed.
shift [ n ]
Rename the positional parameters from $n+1 ... to $1
.... If n is not given, it is assumed to be 1.
test
Evaluate conditional expressions. See test (1) for
usage and description.
times
Print the accumulated user and system times for
processes run from the Shell.
trap [ arg ] [ n ... ]
Read the command arg and execute it when the Shell
receives signal(s) n. (Note that arg is scanned once
when the trap is set and once when the trap is taken.)
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. An
attempt to trap on signal 11 (memory fault) produces an
error. If arg is absent, reset all trap(s) n to their
original values. If arg is the null string, the Shell
and the commands it invokes will ignore the signal. If
n is 0, execute the command arg on exit from the Shell.
With no arguments, print a list of commands associated
with each signal number.
type [ name ... ]
For each name, indicate how it will be interpreted if
used as a command name.
ulimit [-fp [ n ] ]
With the -f option, impose a size limit of n blocks on
files written by child processes (files of any size may
be read). With no n argument, print the current limit.
With the -p option, change the pipe size to n (UNIX/RT
SH-12 Printed 6/10/85
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
only). If no option is given, -f is assumed.
umask [ nnn ]
Set the user file-creation mask to nnn. Refer to umask
(2) for more information. If nnn is omitted, print the
current value of the mask.
unset [ name ... ]
For each name, remove the corresponding variable or
function. The variables PATH, PS1, PS2, MAILCHECK, and
IFS cannot be unset.
wait [ n ]
Wait for the specified process and report its termina-
tion status. If n is not given all currently active
child processes are waited for and the return code is
zero.
INVOCATION
If the Shell is invoked through exec (2) and the first char-
acter of argument zero is a dash (-), commands are initially
read from /etc/profile and from $HOME/.profile, if such
files exist. Thereafter, commands are read as described
below, which is also the case when the Shell is invoked as
/bin/start_sh. The flags below are interpreted by the Shell
on invocation only. Unless the -c or -s flag is specified,
the first argument is assumed to be the name of a file con-
taining commands, and the remaining arguments are passed as
positional parameters to that command file.
-c string Read commands from string.
-s Read commands from the standard input if this flag
is present or no arguments remain. Any remaining
arguments specify the positional parameters.
Write Shell output (except for special commands
above) to file descriptor 2.
-i Make the Shell interactive if this flag is present
or if the Shell input and output are attached to a
terminal. Ignore TERMINATE so that kill 0 does
not kill an interactive Shell, and catch and
ignore INTERRUPT so that wait is interruptible.
In all cases, the Shell ignores QUIT.
-r Make the Shell restricted if this flag is present.
The remaining flags and arguments are described under the
set command listed under SPECIAL COMMANDS.
CAUTIONS
If you execute a command, and 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 perform an exec (2) on the original command.
Use the hash command to correct this situation.
Printed 6/10/85 SH-13
SH(1) DOMAIN/IX Reference Manual (SYS5) SH(1)
EXIT STATUS
Errors detected by the Shell, such as syntax errors, cause
the Shell to return a non-zero exit status. If the Shell is
not being used interactively, execution of the Shell file is
abandoned. Otherwise, the Shell returns the exit status of
the last command executed (see also the exit command under
SPECIAL COMMANDS above).
FILES
/etc/profile
$HOME/.profile
/tmp/sh*
/dev/null
RELATED INFORMATION
cd (1), echo (1), env (1), login (1), pwd (1), test (1),
umask (1), dup (2), exec (2), fork (2), pipe (2), signal
(2), umask (2), wait (2), environ (5).
DOMAIN/IX User's Guide.
SH-14 Printed 6/10/85