Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

csh(1)

ksh(1)

rootnode(1)

test(1)

execve(2)

environ(7)

SH(1)                                BSD                                 SH(1)



NAME
     sh - the Bourne shell command language

SYNOPSIS
     sh [ -ceiknrstuvx ] [ -Dname=value ... ] [ arg ] ...

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

     A list is a sequence of one or more pipelines separated by ;, &, && or ||
     and optionally terminated by ; or &.  ; and & have equal precedence which
     is lower than that of && and ||, && and || also have equal precedence.  A
     semicolon causes sequential execution; an ampersand causes the preceding
     pipeline to be executed without waiting for it to finish.  The symbol &&
     (||) causes the list following to be executed only if the preceding
     pipeline returns a zero (non zero) value.  Newlines may appear in a list,
     instead of semicolons, to delimit commands.

     A command is either a simple-command or one of the following.  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 }  Simply execute 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:  * @ # ? - $ !.  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=value ] ..

     ${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 interpreted as part of its name.  If
          parameter is a digit, it is a positional parameter.  If parameter is
          * or @ then all the positional parameters, starting with $1, are
          substituted separated by spaces.  $0 is set from argument zero when
          the shell is invoked.

     ${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, a standard message is
          printed.

     ${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).
          SHENV
               If this parameter is set, the shell performs parameter
               substitution on the value to generate the pathname of the
               startup script containing commands that the shell executes
               every time a new shell is invoked.  No error results if the
               file specified by the SHENV parameter doesn't exist or can't be
               read.
          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.  IFS is ignored if sh is running as root or if
               the effective user id differs from the real user id.

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.  Explicit null arguments ("" or '') are retained. Implicit null
     arguments (those resulting from parameters that have no values) are
     removed.

FILENAME GENERATION
     Following substitution, sh scans each command word for the characters *,
     ? and [. 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.  The character . at the start of a filename or
     immediately following a /, and the 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 \. 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 \ quotes the following
     characters: \ ' " and $.

     A "$*" is equivalent to "$1 $2 ...", while "$@" is equivalent to "$1"
     "$2"....

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 \ 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 >.

     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 & 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 &.  Otherwise, signals have the values inherited
     by the shell from its parent (see also trap).

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 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 /usr/ucb:/bin:/usr/bin:/usr/apollo/bin.
     If the command name contains a /, 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.

     #    For non-interactive shells, treat everything following the # as a
          comment, i.e., ignore the rest of the line.  For interactive shells,
          the # has no special effect.

     :    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.

     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.

     rootnode [arg]
          Change the current node entry directory to arg.

     set [-ekntuvx [arg ...]]
          -e Exit immediately if a command fails (on some systems, this switch
             works only 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.  The following, used in
             the shell, prints a=b c and c:

                  echo a=b c
                  set -k
                  echo a=b c

          -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
          The positional parameters from $2...  are renamed $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, trap prints 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.3 or sys5.3, 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.
     inlib pathname
          Install a user-supplied library specified by pathname in the current
          (shell) process. The library is used to resolve external references
          of programs (and libraries) loaded after its installation.  Note
          that the library is not loaded into the address space unless it is
          needed to resolve an external reference.  The list of inlibed
          libraries is passed to all children of the current shell. Use
          llib(1) to examine this list.

COMMAND LINE OPTIONS
     If the first character of argument zero is -, 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.

     -Dname=value
                Use the -D option to specify a parameter name, that will be
                set to value, then passed into the shell's environment.  This
                Domain/OS BSD option is useful for tailoring the environment
                of a shell invoked from a program that is not another shell
                (such as the Display Manager). If you set the SHENV parameter
                using this option, the startup script it specifies will be
                run. For example, if you define your <SHELL> key as follows:
                cp /bin/sh -DSHENV=~/.shrc.pad, the ~/.shrc.pad script can
                contain commands to perform special processing for the pad and
                the shell. You can specify more than one -D option.

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

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.

BUGS
     If << is used to provide standard input to an asynchronous process
     invoked by &, 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.

SEE ALSO
     csh(1), ksh(1), rootnode(1), test(1), execve(2), environ(7)

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