This command interpreter can be invoked by typing out sh. It can take a filename as input or can execute commands read directly from the terminal (when called without any arguments).
Syntax:
sh [-ceiknrstuvx] [arg] ...
Invocation:
If the first character of arg[0] is a (-), then commands are read from $HOME/.profile, if such a file exists. The options are interpreted as follows:
-c string | commands are read from string |
---|---|
-s | commands are read from the standard input |
-i | shell is interactive |
-v | print shell input lines as they are read |
For the other shell options, refer to the man pages on sh.
Commands:
The syntax of a command and pipeline
is same as for the C-shell. In addition, a concept called list
is available which is a sequence of one or more pipelines separated by
; & && ||
and optionally terminated by ;
or &
.
; |
causes sequential execution |
& |
causes the preceding pipeline to be executed |
without waiting for it to finish | |
&& (||) |
causes the list following to be executed |
only if the preceding pipeline returns a 0 (non-0)value |
Command Substitution
The standard output from a command enclosed in a pair of backquotes (` `) may be used as part or all of a word; trailing new lines are removed.
Parameter Substitution
The character $ is used to introduce substitutable parameters.
Positional parameters may be assigned values by set. Variables may be set
by writing:
name=value ...
${name}
substitutes the value of the parameter name
The curly braces are required only if another letter immediately follows
name.
${name-word}
substitutes name's value if it is set;
otherwise substitutes word.
${name=word}
sets name to word if not already set and
then substitute; otherwise substitute parameter by its value.
There are other ways of substitution listed in the man pages on sh.
Note that word is not evaluated unless it is to be used as the
substitution string. For example,
% echo ${d-`pwd`}
will only execute pwd if d is not set.
The following parameters are automatically set by the shell:
# |
the number of positional parameters in decimal |
- |
options supplied to the shell on invocation or by set |
? |
the value returned by the last executed command |
$ |
the process number of this shell |
! |
the process numner of the last background command invoked |
The following parameters are used but not set by the shell:
HOME | the default argument for the cd command |
PATH | the search path for commands |
if set to the name of a mail file, the shell | |
informs the user of the arrival of mail in that file | |
PS1 | the primary prompt string (default $) |
PS2 | the secondary prompt string (defaut >) |
IFS | internal field separators, normally space, tab and newline |
After parameter and command substitution, any results of substitution are scanned for internal field separator characters and split into distinct arguments at such points.
Filename Generation
Following substitution, each command word is scanned for the
characters ? [
* . If any of these characters appears, the word is regarded
as a pattern and replaces by the filenames that match the pattern (in alphabetically
sorted fashion), if there is no match the word is left unchanged.
As in the C-shell, . and / must be matched explicitly.
The rules for matching are identical to the C-shell.
Quoting
The following characters have a special meaning to the shell and
cause termination of a word unless quoted - ; | &
( ) > <
newline, s[ace, tab. A character may be quoted by preceding it
with a backslash(\
). All characters enclosed within a pair of quotes (' ')
are quoted. Inside double quotes (" ") parameter and command substitution
occurs and \
quotes the characters \ ' " $
.
For example
"$
*"
is equivalent to "$1 $2 ..."
whereas
"$@"
is equivalent to "$1" "$2"
... .
Input-Output
A command's input and output may be redirected using a special notation interpretd by the shell. In the following uses, substitution occurs before word or digit is used.
<word | use file word as standard input |
---|---|
>word | use file word as standard output |
>>word | append output to end of file word |
<<word | shell input is read upto a line the same as word |
or end of file | |
<& digit |
standard input is duplicated from file descriptor |
digit. Similarly for std. output using >. |
Environment
The environment is a list of name-value pairs that is passed to an
executed program. On invocation the shell scans the environment and creates
a parameter for each valid name found, giving it the corresponding
value. Executed commands inherit the same environment. If the user modifies the
values of these parameters or creates new ones, none of these affects the
environment unless the export command is used to bind the shell's parameter
to the environment. The environment for any single command may be augmented by
prefixing it with one or more assignments to parameters, for eg.,
TERM=450 <cmd> args
Typing set with no arguments gives a list of all the shell parameters that are set along with their values.
Execution
Each time a command is executed the above substitutions are carried
out. Except for the special commands listed below, a new process is created
and an attempt is made to execute the command via execve. The shell
parameter PATH defines the search path for the directory containing the
command. Each alternative directory name is separated by a colon(:). The default
path is :/usr/ucb:/bin:/usr/bin
. If the command name contains a / then
the search path is not used. Otherwise each directory in the path is searched
for an executable file. A subshell is spawned to read it. To deliberately
execute in a subshell, one can parenthesize "( )" the command.
Special Commands
The following commands are executed in the shell process and except where specified no input output redirection is permitted for such commands.
file | read and execute commands from file and return |
---|---|
break[n] | exit from the enclosing for or while loop |
if n is specified, break n levels | |
cd arg | change the current directory to arg |
eval [arg...] | the arguments are read as input to the shell |
and the resulting commands executed | |
exec [arg...] | the comamnd specified by the arg is |
executed in place of the shell without creating a new process | |
exit [n] | causes a non interactive shell to exit with |
the exit status specified by n. If the latter is omitted | |
the exit status is that of the last command executed | |
export [name...] | the given names are exported to the |
environment of subsequently executed commands | |
umask [nnn] | the user file creation mask is set to the octal |
value nnn. In the absence of any argument this | |
gives the current value of the mask. For more information | |
see the section on ULTRIX File Handling. |
Errors detected by the shell cause it to return a non-zero exit status.