Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ sh.bsd(1) — Domain/IX SR9.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

SH(1)

NAME

sh − command language

USAGE

sh [ args ] ... 

DESCRIPTION

Sh is a command programming language that executes commands read from a terminal or a file. A simple command is a sequence of nonblank words separated by blanks.  A blank is a tab or a space. The first word specifies the name of the command to be executed. Except as specified below, the remaining words are passed as arguments to the invoked command. The command name is passed as argument 0; refer to execve (2) for further details.  The value of a simple command is its exit status if it terminates normally, or 200+status if it terminates abnormally.  See sigvec (2) for a list of status values. 

A pipeline is a sequence of one or more commands separated by the pipe character (|).  The standard output of each command but the last is connected by 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. 

A list is a sequence of one or more pipelines separated by a semicolon (;), an ampersand (&), double ampersand (&&), or double pipe (||).  Lists are optionally terminated by a semicolon (;) or an ampersand (&).  A single semicolon or ampersand has equal precedence; the same holds true for the double ampersand and double pipe (although these characters have higher precedence over the former).  A semicolon causes sequential execution; an ampersand causes the preceding pipeline to be executed without waiting for it to finish.  A double ampersand or double pipe causes the list following to be executed only if the preceding pipeline returns a zero (nonzero) value.  Newlines may appear in a list, instead of semicolons, to delimit commands. 

A command is either a simple command or one of the ones listed below.  The value returned by a command is that of the last simple command executed in the command. 

COMMANDS

for name [in word ...] do list done
Set name to the next word in the for word list each time a for command is executed.  If in word ...  is omitted, in $@ is assumed.  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. 

if list then list [elif list then list] ... [else list] fi
Execute the list following if, and if it returns zero, execute the list following then.  Otherwise, execute the list following elif, and if its value is zero, execute the list following then.  Failing that, execute the else list. 

while list [do list] done
Repeatedly execute the while list, and if its value is zero, execute the do list; otherwise, terminate the loop.  The value returned by a while command is that of the last executed command in the do list.  To negate the loop termination test, use until in place of while. 

( list )
Execute list in a sub-Shell. 

{ list }
Do a simple execution of list. 

Sh recognizes the following words only when they are the first word of a command, and only when they are not quoted:

if then else elif fi case in esac for while until do done { }

COMMAND SUBSTITUTION

Use the standard output from a command, enclosed in a pair of back quotes (``), as part or all of a word.  Trailing newlines are removed. 

PARAMETER SUBSTITUTION

A parameter is a sequence of letters, digits, or underscores (a name), a digit, or any of the following characters: * @ # ? − $ !. A dollar sign ($) introduces parameters that may be substituted.  You can assign positional parameters by using set.  The following shows how to set variables:

name=value [ name=value ] ... 

${parameter}
Substitute the value, if any, of the parameter.  You must use braces only when parameter is followed by a letter, digit, or underscore that is not to be interpreted as part of its name.  If parameter is a digit, it is a positional parameter.  If parameter is an asterisk (*) or an at sign (@), then substitute all the positional parameters separated by spaces (starting with $1).  A $0 is set from argument zero when you invoke the Shell. 

${parameter−word}
If parameter is set, substitute its value; otherwise, substitute word. 

${parameter=word}
If parameter is not set, set it to word; then, substitute the value of the parameter.  You may not assign positional parameters in this manner. 

${parameter?word}
If parameter is set, substitute its value; otherwise, print word and exit from the Shell.  If word is omitted, print a standard message. 

${parameter+word}
If parameter is set, substitute word; otherwise, substitute nothing. 

In the above, sh does not evaluate word unless it is to be used as the substituted string.  Thus, for example, echo ${d−´pwd´} only executes pwd if d is unset. 

The following parameters are automatically set by the Shell:

# Number of positional parameters in decimal. 

− Options supplied to the Shell on invocation or by set. 

?  Value returned by the last executed command in decimal. 

$ Process number of this Shell. 

!  Process number of the last background command invoked. 

The following parameters are used but not set by the Shell:

HOME
Default argument (home directory) for the cd command. 

PATH The search path for commands (see EXECUTION). 

MAIL If this variable is set to the name of a mail file, the Shell informs you of the arrival of mail in the specified file. 

PS1 Primary prompt string; this is a dollar sign ($) by default. 

PS2 Secondary prompt string; this is a greater-than sign (>) by default. 

IFS Internal field separators; these usually include space, tab, and new-line.

BLANK INTERPRETATION

After parameter and command substitution, sh scans the subsequent results for internal field separator characters (those found in $IFS, and then splits its output into distinct arguments where such characters are found.  Sh retains explicit null arguments.  It removes those that are implicit (i.e., resulting from parameters that have no values). 

FILENAME GENERATION

Following substitution, sh scans each command word for asterisks (*), question marks (?), and left brackets ([).  If one of these characters appears, it regards the word as a pattern.  Sh then replaces the word with alphabetically sorted filenames that match the pattern.  If no filename that matches the pattern is found, the word is left unchanged.  If there is a period at the start of a filename, or immediately following a slash character (/), the slash character must be matched explicitly. 

* Matches any string, including the null string. 

?  Matches any single character. 

[...] Matches any one of the characters enclosed.  A pair of characters separated by − matches any character lexically between the pair. 

QUOTING

The following characters have a special meaning to the Shell and cause termination of a word unless quoted:

;   &   (   )   |   <   >   newline   space   tab

You may quote a character by preceding it with a backslash (\).  Sh ignores a \newline.  All characters enclosed between a pair of single quotation marks (´´), except a single quote, are quoted.  Parameter and command substitution occurs inside double quotes (“”).  A backslash quotes the following characters: \ ´ " and $.

A $* is equivalent to a $1 $2 ... construct, while $@ is equivalent to a $1 $2 ... pattern. 

PROMPTING

When used interactively, sh prompts with the value of PS1 before reading a command.  If, at any time, you type a newline and need further input to complete a command, sh issues the secondary prompt ($PS2). 

INPUT/OUTPUT

Before you execute a command, you may redirect its output by using a special notation interpreted by the Shell.  The following may appear anywhere in a simple command 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 descriptor 0). 

>word
Use file word as standard output (file descriptor 1).  If the file does not exist, create it; otherwise, truncate it to zero length. 

>>word
Use file word as standard output.  If the file exists, append output (by seeking to the end); otherwise, create the file. 

<<word
Read the Shell input up to a line the same as word or end-of-file.  Make the resulting document the standard input.  If any character of word is quoted, place no interpretation upon the characters of the document; otherwise, do parameter and command substitution, ignore, newline, and use a backslash (\) to quote the following characters: \ $ ´ and the first character of word. 

<&digit
Duplicate the standard input from file descriptor digit.  This works similarly for the standard output using >. Refer to dup(2) for further details. 

<&− Close the standard input.  Perform the same function on the standard output, using a greater-than symbol (>).

If one of the above is preceded by a digit, the file descriptor created is that specified by the digit (instead of the default 0 or 1).  For example,

... 2>&1

creates file descriptor 2 to be a duplicate of file descriptor 1. 

If a command is followed by an ampersand (&), the default standard input for the command is the empty file (/dev/null). Otherwise, the environment for the execution of a command contains the file descriptors of the invoking Shell as modified by input/output specifications.

ENVIRONMENT

The environment is a list of name-value pairs that is passed to an executed program in the same way as a normal argument list.  Refer to execve (2) and environ (7).  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.  Executed commands inherit the same environment.  If you modify the values of these parameters, or create new ones, the environment is unaffected, unless you use the export command to bind the Shell’s parameter to the environment.  The environment seen by any executed command is thus composed of any unmodified name-value pairs originally inherited by the Shell, plus any modifications or additions, all of which must be noted in export commands. 

You may augment the environment for any simple command by prefixing it with one or more assignments to parameters.  Thus, the following two lines are equivalent:

TERM=450 cmd args
(export TERM; TERM=450; cmd args)

SIGNALS

Sh ignores the INTERRUPT and QUIT signals for an invoked command if the command is followed by an ampersand (&).  Otherwise, signals have the values inherited by the Shell from its parent. 

EXECUTION

Each time you execute a command, sh carries out the above substitutions.  Except with the special commands listed below, it creates a new process and attempts to execute the command via an execve (2). 

The $PATH Shell parameter defines the search path for the directory containing the command.  Each alternative directory name is separated by a colon (:).  The default path is :/bin:/usr/bin .  If the command name contains a slash (/), sh does not use the search path.  Otherwise, sh searches each directory in the path 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 commands.  A sub-Shell (i.e., a separate process) is spawned to read it.  A command in parentheses is also executed in a sub-Shell. 

SPECIAL COMMANDS

The following commands are executed in the Shell process, and except where specified, no input/output redirection is permitted for such commands. 

: No effect; the command does nothing. 

. file Read and execute commands from file and return.  Use the search path $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. 

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 $HOME Shell parameter is the default arg.

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. You may supply input/output arguments, and if you give no other type of arguments, sh modifies the input/output. 

exit [n]
If the Shell is not interactive, exit with the exit status specified by n.  If you omit n, the exit status is that of the last command executed.  (An end-of-file will also exit from the Shell.) 

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 exportable names. 

inlib pathname
Install a user-supplied library specified by pathname at the current Shell program level.  The library remains installed until the Shell that installed it exits.  User-inlibed libraries are only available to programs that are run in-process.  Look under the description of in-process execution given in the predefined variable section of this manual entry.  See also the description of the DOMAIN command INLIB in the DOMAIN System Command Reference. 

login [arg ...]
Equivalent to an exec login arg ... command. 

read name ... 
Read one line from the standard input.  Assign successive words of the input to the variables name in order, with leftover words to the last variable.  The return code is 0 unless the end-of-file is encountered. 

readonly [name ...]
Mark the given names as read-only.  You cannot change the values of these names by subsequent assignment. If no arguments are given, print a list of all read-only names.

set [−ekntuvx [arg ...]]

−e Exit immediately if a command fails (this switch only works on Shells that are not interactive). 

−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 substituting. 

−v Print Shell input lines as they are read. 

−x Print commands and their arguments as they are executed. 

− Turn off the −x and −v options. 

You can also use these options when you first invoke the Shell.  The current set may be found in $− . 

Remaining arguments are positional parameters.  The Shell assigns them, in order, to $1, $2, etc.  If no arguments are given, it prints the values of all names. 

shift Rename the positional parameters from $2 ... to $1 ... 

times Print the accumulated user and system times for processes run from the Shell. 

trap [arg] [n] ...
Read and execute arg upon receipt of signal(s) n.  (Scan arg once when the trap is set and once when the trap is taken.)  Execute trap commands in order of signal number.  If arg is absent, reset all trap(s) n to their original values.  If arg is the null string, the Shell and invoked commands ignore this signal.  If n is 0, execute the command arg on exit from the Shell.  Otherwise, execute the command upon receipt of signal n as numbered in sigvec (2).  With no arguments, print a list of commands associated with each signal number. 

umask [ nnn ]
Set the user file creation mask to the octal value nnn.  See umask (2) for details.  If nnn is omitted, print the current value of the mask. 

ver

ver systype
With no arguments, return the current value of the SYSTYPE environment variable that specifies the version of UNIX commands being executed by the Shell.  With a systype argument, change the SYSTYPE environment variable to either bsd4.2 or sys5, depending on which is specified. 

wait [n]
Wait for the specified process and report its termination status.  If n is not given, wait for all currently active child processes.  The return code from this command is that of the process being awaited. 

COMMAND LINE OPTIONS

If the first character of argument zero is a dash (−), sh reads commands from $HOME/.profile, if such a file exists. It then reads commands as described below. The following options are interpreted by the Shell when it is invoked.

−c string Read commands from string.

−s Read commands from the standard input.  Write Shell output to file descriptor 2.  (Note that the same activity occurs if no arguments remain.) 

−i Make the Shell interactive.  (Note that this also occurs if the Shell input and output are attached to a terminal, as told by gtty.) Ignore the terminate signal SIGTERM, so that kill 0 does not kill the interactive Shell.  Catch and ignore the interrupt signal SIGINT, so that wait is interruptible.  In all cases, ignore SIGQUIT. 

The remaining flags and arguments are described under the set command. 

CAUTIONS

If you use << to provide standard input to an asynchronous process invoked by an ampersand (&), the Shell gets confused about naming the input document.  It creates a  garbage file called /tmp/sh* and complains about not being able to find the file by another name. 

EXAMPLES

The following, used in the Shell, prints a=b c and c:

echo a=b c
set −k
echo a=b c

FILES

$HOME/.profile
/tmp/sh*
/dev/null

DIAGNOSTICS

Errors detected by the Shell, such as those that occur in syntax, cause sh to return a nonzero exit status.  If the Shell is not being used interactively, then execution of the Shell file is abandoned.  Otherwise, the exit status of the last command executed is returned.  Refer to exit under SPECIAL COMMANDS for more information. 

RELATED INFORMATION

test (1), execve (2), environ (7). 

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026