man(1) Manual page archive


     SH(1)                                                       SH(1)

     NAME
          sh, cd, wait, whatis - shell, the standard command
          programming language

     SYNOPSIS
          sh [ -acefiknpstuvx ] [ args ]

     DESCRIPTION
          Sh is a command programming language that executes commands
          read from a terminal or a file.  See `Invocation' below for
          the meaning of arguments to the shell.

        Definitions
          A blank is a tab or a space.  A name is a sequence of let-
          ters, digits, or underscores beginning with a letter or
          underscore.  A parameter is a name, a digit, or any of the
          characters *, @, #, ?, -, $, and !.  A word is a sequence of
          characters and quoted strings set off by operators, blanks,
          or newlines; see `Quoting'.

        Commands
          A simple-command is a sequence of words separated by blanks.
          The first word specifies the name of the command to be exe-
          cuted.  Except as specified below, the remaining words are
          passed as arguments to the invoked command.  The command
          name is passed as argument 0; see exec(2). The value of a
          simple-command is its exit status if it terminates normally,
          or 0200+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 |.  If there is more than one command, each is run in a
          subshell; | denotes a pipe(2) connecting the standard output
          of one command to the standard input of the next.  Each com-
          mand 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 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 pipeline; the shell does not wait and pro-
          ceeds as if the pipeline had returned zero exit status.  The
          symbol && (||) causes the list following it to be executed
          only if the preceding pipeline returns a zero (non-zero)
          exit status.  One or more newlines may follow any sequencing
          operator (; & && ||).

     SH(1)                                                       SH(1)

          One or more newlines may always be used in place of a single
          semicolon, and newlines may be freely inserted after any of
          | ; & && || ;; if do then elif else fi done while until.

          A command is either a simple-command or one of the follow-
          ing.  Unless otherwise stated, the value returned by a com-
          mand is that of the last simple-command executed in the com-
          mand.

          for name [ in word ... ; ] do list ; done
               A `for' command executes a list of commands once for
               each word, with name set to each word in turn.  If in
               word ... ; is omitted or replaced by newlines, then the
               list is executed once for each positional parameter
               that is set; see `Parameter Substitution'.
          case word in [ pattern [ | pattern ] ... ) list ;; ] ... esac
               A `case' command executes the list associated with the
               first pattern that matches word. The form of the pat-
               terns is the same as that used for file-name generation
               (see `File Name Generation') except that a slash, a
               leading dot, or a dot immediately following a slash
               need not be matched explicitly.  Newlines may precede
               each pattern and replace the last ;; before esac.
          if list then list [ elif list then list ] ... [ else list ] fi
               The list following `if' is executed and, if it returns
               a zero exit status, the list following the first `then'
               is executed.  Otherwise, the lists in `elif' clauses
               are executed in turn until one returns zero status;
               then the list following the next `then' is executed.
               Otherwise, the `else' list is executed.  If no `else'
               list or `then' list is executed, then the if command
               returns a zero exit status.
          while list do list done
               A `while' command repeatedly executes the `while' list
               and, if the exit status of the last command in the list
               is zero, executes the `do' list; otherwise the loop
               terminates.  If no commands in the `do' list are exe-
               cuted, then the `while' command 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}
               list is simply executed.
          name () command
               Define a function which is referenced by name. The body
               of the function is the command. The most useful form of
               command is a sequence of commands enclosed in braces {
               }.  Execution of functions is described under Execution
               below.

          These words are only recognized as the first word of a

     SH(1)                                                       SH(1)

          command and when not quoted: if then else elif fi case esac
          for while until do done.

        Comments
          A word beginning with # causes that word and all the follow-
          ing characters up to a newline to be ignored.

        Command Substitution
          The standard output from a command enclosed in a pair of
          grave accents ` ` may be used as part or all of a word;
          trailing newlines are removed.

        Parameter Substitution
          The character $ is used to introduce 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}
               The value, if any, of the parameter is substituted.
               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 * or
               @, all the positional parameters, 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; the
               value of the parameter is substituted.  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, the message ``parameter null or not
               set'' is printed.
          ${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:

     SH(1)                                                       SH(1)

               echo ${d:-`pwd`}

          If the colon (:) is omitted from the above expressions, the
          shell only checks whether parameter is set or not.

          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 syn-
                    chronously executed command; see exit(2).
               $    The process number of this shell.
               !    The process number of the last background command
                    invoked.

          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 `Execution'.
               CDPATH
                    The search path for the cd command.
               MAIL If this parameter is set to the name of a mail
                    file the shell informs the user of the arrival of
                    mail in the specified file.  The file is inspected
                    every three minutes.
               HISTORY
                    If this parameter is set to the name of a writable
                    file, the shell appends interactive input to the
                    file, for use by the command =(1).
               PS1  Primary prompt string, by default `$'.
               PS2  Secondary prompt string, by default `>'.
               IFS  Internal field separators, normally space, tab,
                    and newline.

          The shell gives default values to PATH, PS1, PS2 and IFS.
          HOME is set by login(8).

        Blank Interpretation
          After parameter and command substitution, the results of
          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 ("" or '') are retained.  Implicit null arguments
          (those resulting from parameters that have no values) are
          removed.

        File Name Generation
          Following substitution, each command word is scanned for the
          characters *, ?, and [.  If one of these characters appears
          the word is regarded as a pattern. The word is replaced with
          alphabetically sorted file names that match the pattern.  If
          no file name is found that matches the pattern, the word is

     SH(1)                                                       SH(1)

          left unchanged.  The directories . and .. (initially or
          after a /) are only matched by patterns beginning with an
          explicit period.  The character / itself must be matched
          explicitly.

               *    Matches any string, including the null string.
               ?    Matches any single character.
               [...]
                    Matches any one of the enclosed characters.  A
                    pair of characters separated by - matches any
                    character lexically between the pair, inclusive.
                    If the first character following the opening `['
                    is a `^' any character not enclosed is matched.

        Quoting
          These characters have a special meaning to the shell and
          terminate a word unless quoted:

               ;  &  (  )  |  <  >  { } newline space tab

          (The characters { and } need not be quoted inside a ${} con-
          struction.)  A character may be quoted (i.e., made to stand
          for itself) by preceding it with a \.  The pair \newline is
          ignored.  All characters enclosed between a pair of single
          quote marks '' (except a single quote) are quoted.  Inside
          double quote marks "" parameter and command substitution
          occurs and \ quotes the characters \, `, ", and $.  "$*" is
          equivalent to "$1 $2 ...", whereas "$@" 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
          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 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

     SH(1)                                                       SH(1)

                        seeking to the end-of-file); otherwise, the
                        file is created.
          <<word        The shell input is read 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 inter-
                        pretation is placed upon the characters of the
                        document; otherwise, parameter and command
                        substitution occurs, (unescaped) \newline is
                        ignored, and \ must be used to quote the char-
                        acters \, $, `, and the first character of
                        word.
          <&digit       Use the file associated with file descriptor
                        digit as standard input.  Similarly for the
                        standard output using >&digit.
          <&-           The standard input is closed.  Similarly for
                        the standard output using >&-.

          If any of the above is preceded by a digit, the file
          descriptor which 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', then
          associates file descriptor 2 with the same file as descrip-
          tor 1, namely `xxx', while

               ... 2>&1 1>xxx

          associates file descriptor 2 with the current value of file
          descriptor 1 (typically the terminal) and file descriptor 1
          with `xxx'.

          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 strings, conventionally

     SH(1)                                                       SH(1)

          function definitions and name-value pairs, that is passed to
          an executed program in the same way as a normal argument
          list; see environ(5). The shell interacts with the environ-
          ment in several ways.  On invocation, the shell scans the
          environment and creates a parameter or function for each
          name found, giving it the corresponding value.  If the user
          modifies the value of any of these parameters or creates new
          parameters, none of these affects the environment unless the
          export command is used to bind the shell's parameter to the
          environment; see also set -a.  A parameter may be removed
          from the environment with the unset command.  The environ-
          ment 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 modifica-
          tions or additions, all of which must be noted in export
          commands.

          The environment for any simple-command may be augmented by
          prefixing it with one or more assignments to parameters (but
          not functions).  Thus `tabs' gets the same environment in
          both lines below, but the shell has one less variable in the
          second.

               (export TERM; TERM=450; tabs)
               TERM=450 tabs

          If the -k flag is set, all keyword arguments are placed in
          the environment, even if they occur after the command name.

        Signals
          SIGINT and SIGQUIT (see signal(2)) for an invoked command
          are ignored if the command is followed by &; otherwise sig-
          nals have the values inherited by the shell from its parent
          (but see also the trap command below).

        Execution
          Each time a command is executed, the above substitutions are
          carried out.  If the command name matches the name of a
          defined function, the function is executed in the shell pro-
          cess.  (Note how this differs from calling a shell script.)
          The positional parameters $1, $2, ....  are set to the argu-
          ments of the function.  If the command name does not match a
          function, but matches one of the builtin commands listed
          below, it is executed in the shell process.  If the command
          name matches neither a builtin command nor 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

     SH(1)                                                       SH(1)

          /usr/bin, in that order).  Note that the current directory
          is specified by a null path name, 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 / the search path is not used.  Otherwise, each
          directory in the path is searched for an executable file.
          If the file has execute permission but is not executable by
          exec(2), it is assumed to be a `shell script', a file of
          shell commands.  A sub-shell is spawned to read it.  A
          parenthesized command is also executed in a sub-shell.

        Builtin Commands
          Input/output redirection is permitted for these commands.
          File descriptor 1 is the default output location.

          :    No effect; the command does nothing.  A zero exit code
               is returned.
          . file
               Read and execute commands from file and return.  The
               search path specified by PATH is used to find the
               directory containing file.
          builtin [ command ]
               Execute the builtin command (such as break) regardless
               of functions defined with the same name.
          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 n-th 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 containing
               arg. Alternative directory names are separated by a
               colon (:).  The current directory (default) is speci-
               fied by a null path name, which can appear immediately
               after the equal sign or between the colon delimiters
               anywhere else in the path list.  If arg begins with a
               `/' the search path is not used.  Otherwise, each
               directory in the path is searched for arg.
          eval [ arg ... ]
               The arguments are read as input to the shell and the
               resulting command(s) executed.
          exec [ arg ... ]
               The non-builtin command specified by the arguments is
               executed 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 ]

     SH(1)                                                       SH(1)

               Causes a shell to exit with the exit status specified
               by n. If n is omitted the exit status is that of the
               last command executed (an end-of-file will also cause
               the shell to exit.)
          export [ name ... ]
               The given names are marked for automatic export to the
               environment of subsequently-executed commands.  If no
               arguments are given, a list of all names that are
               exported in this shell is printed.
          read [ name ... ]
               One line is read from the standard input and the first
               word is assigned 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.
          return [ n ]
               Causes a function to exit with the return value speci-
               fied by n. If n is omitted, the return status is that
               of the last command executed.
          set [ --aehknptuvx [ arg ... ] ]
               -a   Mark variables which are modified or created for
                    export.
               -e   Exit immediately if a command exits with a non-
                    zero exit status.
               -f   Disable file name generation
               -k   All keyword arguments are placed in the environ-
                    ment for a command, not just those that precede
                    the command name.
               -n   Read commands but do not execute them.
               -p   Remove the definitions for all functions imported
                    from the environment, and set IFS to blank, tab
                    and newline.
               -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; useful in setting
                    $1 to -.
               Using + rather than - causes these flags to be turned
               off.  These flags can also be used upon invocation of
               the shell.  The current set of flags may be found in
               $-.  The remaining arguments are positional parameters
               and are assigned, in order, to $1, $2, ....  If no
               arguments are given the values of all names are
               printed.
          shift [ n ]
               The positional parameters from $n+1 ...  are renamed $1
               ...  If n is not given, it is assumed to be 1.
          times
               Print the accumulated user and system times for

     SH(1)                                                       SH(1)

               processes run from the shell.
          trap [ arg ] [ n ] ...
               The command arg is to be read and executed 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.
               If arg is absent all traps n are reset to their origi-
               nal values.  If arg is the null string this signal is
               ignored by the shell and by the commands it invokes.
               If n is 0 the command arg is executed on exit from the
               shell.  The trap command with no arguments prints a
               list of commands associated with each signal number.
          umask [ nnn ]
               The user file-creation mask is set to nnn; see
               umask(2). If nnn is omitted, the current value of the
               mask is printed.
          unset [ name ... ]
               For each name, remove the corresponding variable or
               function.  The variables PATH, PS1, PS2 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.
          whatis [ name ... ]
               For each name, print the associated value as a parame-
               ter, function, builtin or executable file as appropri-
               ate.  In each case, the value is printed in a form that
               would yield the same value if typed as input to the
               shell itself: parameters are printed as assignments,
               functions as their definitions, builtins as calls to
               builtin, and executable files as completed pathnames.

        Invocation
          Normally the shell reads commands from the file named in its
          first argument (standard input default).  The remaining
          arguments are interpreted as position parameters; see
          `Parameter substitution' above.  If the shell is invoked
          through exec(2) and the first character of argument zero is
          -, commands are read first from $HOME/.profile, if it
          exists.  Certain options modify this behavior:

          -c string Read commands from string; ignore remaining argu-
                    ments.
          -s        Write shell output (except for builtin commands)
                    on file descriptor 2.
          -i        Interactive.  Ignore signal SIGTERM (interactive
                    shell is immune to kill 0).  Catch and ignore
                    SIGINT (wait is interruptible).  The shell always

     SH(1)                                                       SH(1)

                    ignores SIGQUIT.

          Other options are described under the set command above.

     FILES
     SEE ALSO
          =(1), echo(1), newgrp(1), test(1), dup(2), exec(2), fork(2),
          pipe(2), signal(2), umask(2), exit(2), environ(5)
          B. W. Kernighan and R. Pike, The Unix Programming
          Environment, Prentice-Hall, 1984

     DIAGNOSTICS
          Errors detected by the shell, such as syntax errors, cause
          the shell to return a non-zero exit status.  If the shell is
          being used non-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.

     BUGS
          Errors arising from builtins terminate shell scripts.