Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ set(1) — Reliant UNIX 5.44c4

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

env(1)

getopt(1)

ksh(1)

sh(1)

set(1)                                                               set(1)

NAME
     set - set shell options or positional parameters

SYNOPSIS
     set [option ...] [argument ...]

DESCRIPTION
     The shell built-in command set has three functions:

     ⊕  If no operand is specified, set writes on standard output the names
        and values of all shell variables defined for the current shell.

        By contrast, the env command writes on standard output the names
        and values of all shell variables available to all commands and all
        subshells: variables such as IFS, MAILCHECK, PATH, PS1 and PS2 are
        not included unless they have been exported (with export).

     ⊕  Any options assigned to set govern the behavior of the current
        shell.

        The same options are also available for the sh or ksh command, but
        in this case they govern the behavior of the subshell thus gen-
        erated.

     ⊕  Arguments used instead of an option or after the option list are
        handled by set as follows:

        -  it assigns the first nine arguments to positional parameters $1
           through $9,

        -  it assigns all the arguments to the shell parameters $* and $@,

        -  it assigns the number of arguments to the shell parameter $#.

        If there are fewer than nine arguments, the remaining positional
        parameters are set to the null string.

        To access the tenth and subsequent command-line arguments directly
        you need to use the shift command.

     If you want to use set to control the execution of a shell script, you
     have to include set in the script. This is because shell scripts are
     always executed by a subshell.

OPTIONS
     No option specified:
          set sequentially assigns the first nine arguments to positional
          parameters $1 through $9. It assigns all command-line arguments
          collectively to the shell parameters $@ and $*; and it assigns
          the total number of arguments to the shell parameter $#. If there
          are fewer than nine arguments, the remaining positional parame-
          ters are set to the null string.



Page 1                       Reliant UNIX 5.44                Printed 11/98

set(1)                                                               set(1)

          To access the tenth and subsequent command-line arguments
          directly you need to use the shift command.

     No operand specified:
          set writes on standard output the names and values of all shell
          variables defined for the current shell.

     option
          The options govern the behavior of the current shell.

          If you want to use more than one option, you have to group the
          code letters together with no spaces between them and with a sin-
          gle minus sign at the beginning.

          The -- option cannot be combined with other options.

          You can use the command echo $- to find out which options have
          already been set with the set or sh or ksh commands. Set options
          other than -n and -t can also be unset without the need to ter-
          minate the current shell.

          The Korn shell ksh has additional options [see ksh(1)].

     The following options are available:

     -a   Automatically exports all shell variables that you define or
          redefine in the current shell.

          -a not specified and not set previously:

          Shell variables that you define or redefine in the current shell
          are not automatically exported. In other words, a shell variable
          will not be known in a subshell unless you explicitly export it
          using the shell built-in export.

     +a   Turns off option -a. Any shell variables defined earlier while
          the -a option was active will continue to be exported automati-
          cally.

     -e   The current shell exits immediately if a command returns a non-
          zero exit status.

          -e not specified and not set previously:

          An interactive shell exits when you press CTRL-D. A shell running
          a shell script only exits prematurely if it encounters a syntax
          error. Otherwise it exits on reaching the end of the script,
          regardless of the exit status of individual commands.

     +e   Turns off option -e.




Page 2                       Reliant UNIX 5.44                Printed 11/98

set(1)                                                               set(1)

     -f   Disables the file name generation mechanism. The characters *, ?
          and [...] are not treated as metacharacters and expanded to gen-
          erate all the matching file names.

          -f not specified and not set previously:

          Matching file names are substituted for the above patterns.

     +f   Turns off option -f.

     -h   This option changes the behavior of the current shell when you
          define shell functions: The commands you include in a shell func-
          tion are entered in the hash table at the time of function defin-
          ition [see hash(1)].

          -h not specified and not set previously:

          Commands used in a shell function are not entered in the hash
          table until the function is executed.

     +h   Turns off option -h.

     -k   (Keyword) Variable assignments, i.e. arguments in the form
          name=value, are allowed at any point on the command line. The
          shell performs the assignment and makes the resultant keyword
          parameter available to the environment of the command (see Exam-
          ple 1).

          -k not specified and not set previously:

          Variable assignments must precede the command name. The shell
          makes the appropriate keyword parameters available to the envi-
          ronment of the command.

     +k   Turns off option -k.

     -n   The current shell reads and interprets all commands but does not
          execute them, thus omitting the last step in the processing of a
          command line [see sh(1), COMMAND LINE PROCESSING].

          The -n option can be used to check a shell script for syntax
          errors.

          Caution:
               If you enter set -n interactively, the only way to turn off
               the -n option is to exit the shell with CTRL-D.

               The -n option cannot be turned off within a shell script. It
               is turned off automatically when the shell running the
               script exits.




Page 3                       Reliant UNIX 5.44                Printed 11/98

set(1)                                                               set(1)

          -n not specified and not set previously:

          All commands are read, interpreted, and executed.

     -t   Terminate. The current shell exits after executing one command,
          i.e. the command line that contains the set -t command.

          -t not specified:

          An interactive shell exits when you press CTRL-D. A shell running
          a shell script only exits prematurely if it encounters a syntax
          error. Otherwise it exits on reaching the end of the script.

          If the -e option is set in the current shell, this shell exits
          immediately if a command returns a non-zero exit status.

     -u   Causes the current shell to issue an error message on encounter-
          ing an unset shell variable in a command line. The command in
          question is not executed.

          Shell scripts are terminated as soon as the shell encounters an
          undefined shell variable.

          -u not specified and not set previously:

          The shell does not issue an error message for an unset shell
          variable, but substitutes the null string as its value by
          default.

     +u   Turns off option -u.

     -v   Verbose. The current shell displays each subsequent input on
          standard error before executing the corresponding command.

          In combination with the -x option, this option is useful for
          debugging shell scripts.

          -v not specified and not set previously:

          The current shell does not echo its input.

     +v   Turns off option -v.

     -x   The current shell interprets its input and writes it on the stan-
          dard error, marking each command that it processes with a plus
          sign (+) at the start of the line. A command consisting solely of
          a variable assignment is output without the plus sign. The shell
          then executes the command.

          In contrast to the output with option -v, the -x option shows you
          how the shell has replaced metacharacters and shell variables.



Page 4                       Reliant UNIX 5.44                Printed 11/98

set(1)                                                               set(1)

          Thus you can test a shell script as follows:

          -  either include set -xv as the first command in your shell
             script, or

          -  run the shell script with the command sh -xv script.

          -x not specified and not set previously:

          The current shell does not echo its input.

     +x   Turns off option -x.

     --   Not combinable with other options.

          The first argument specified after -- is not interpreted as an
          option, even if it begins with a minus sign. This enables you to
          use set to assign values beginning with a minus sign to posi-
          tional parameters (see Example 4).

          set -- has no effect on any other options currently in effect. In
          the Korn shell ksh however, all positional parameters are
          deleted.

     argument
          Any string delimited by blanks or tabs. The last argument is ter-
          minated by a command separator.

          You can include any number of arguments, with at least one blank
          or tab between them.

          set sequentially assigns the first nine arguments to positional
          parameters $1 through $9. It assigns all command-line arguments
          collectively to the shell parameters $@ and $*; and it assigns
          the total number of arguments to the shell parameter $#. If there
          are fewer than nine arguments, the remaining positional parame-
          ters are set to the null string.

          To access the tenth and subsequent command-line arguments
          directly you need to use the shift command.

          argument not specified:

          The positional parameters $1 through $9 and the shell parameters
          $@, $* and $# are left unchanged.

LOCALE
     The LCMESSAGES environment variable governs the language in which
     message texts are displayed. If LCMESSAGES is undefined or is defined
     as the null string, it defaults to the value of LANG. If LANG is like-
     wise undefined or null, the system acts as if it were not internation-
     alized.


Page 5                       Reliant UNIX 5.44                Printed 11/98

set(1)                                                               set(1)

     The LCALL environment variable governs the entire locale. LCALL
     takes precedence over all the other environment variables which affect
     internationalization.

EXAMPLES
     Example 1

     The shell script archive contains the following:

     set | grep customer
     echo $customer

     The -k option is set in the current shell, and the shell script is
     then executed as follows:

     $ set -k
     $ sh archive customer=Meyer
     customer=Meyer
     Meyer

     After set -k, the shell variable customer may be defined after the
     script name in the command line. This variable, which is accessed in
     the example by the echo and set commands, is only known in the sub-
     shell that runs the script.

     When the script finishes, the subshell that runs it also exits. The
     variable customer is not known in the (now current) parent shell. The
     command below will therefore output nothing:

     $ set | grep customer

     If the -k option is now turned off with +k and the shell script is
     invoked as before, the customer variable will be undefined within the
     script. The echo command simply outputs a blank line.

     In other words, if the -k option is not set, the variable definition
     must precede the script name on the command line.

     $ set +k
     $ sh archive customer=Meyer
     $ customer=Meyer sh archive
     customer=Meyer
     Meyer

     Example 2

     The shell script evaltest has the following contents:

     set -xv
     eval echo \$$#
     echo $0



Page 6                       Reliant UNIX 5.44                Printed 11/98

set(1)                                                               set(1)

     The set command sets the -x and -v options in the subshell that exe-
     cutes this shell script. The shell script is then called as shown
     below:

     $ sh evaltest today is friday
     eval echo \$$#
     + eval echo $3
     + echo friday
     friday
     echo $0
     + echo evaltest
     evaltest

     The -v option (verbose) causes each command to be displayed before it
     is processed.

     The -x option causes the shell to interpret each command line and out-
     put it with a + in the first column. The output shows that the
     command-line arguments of the eval command are interpreted twice by
     the shell.

     Example 3

     The tape of a mounted tape cartridge is not to be retensioned before
     the first access. The mt(1) command takes the name of the special file
     from the shell variable TAPE.

     The contents of the corresponding shell script mtno are given below:

     set -u
     mt -f $TAPE noret

     Since the -u option is set, the mt command is executed only if the
     shell variable has been defined. This enables you to prevent mt from
     accessing the default special file /dev/rmt0.

     If the TAPE variable has not been defined, the shell script will ter-
     minate immediately with the following error message:

     $ sh mtno
     mtno: TAPE: parameter not set

     Example 4

     The following shell script counts all arguments read from the standard
     input. It is, however, somewhat slower than the wc -w command:

     1  : sh count counts all arguments on standard input
     2  num=0
     3  while read line
     4  do
     5     set -- $line


Page 7                       Reliant UNIX 5.44                Printed 11/98

set(1)                                                               set(1)

     6     num=`expr $num + $#`
     7  done
     8  echo $num

     Line 3:

     The shell built-in read reads a line from the standard input and
     assigns it to the variable line.

     Line 5:

     The shell built-in set assigns the arguments which make up the value
     of the variable line to the positional parameters $1, $2 and so on; $#
     is the total number of arguments in $line.

     In this example the -- option has two functions:

     -  Even if $line is empty, set still does not output the current envi-
        ronment. (In the Korn shell ksh, all available positional parame-
        ters are deleted.)

     -  If $line starts with a minus sign, set does not interpret its first
        argument as an option.

     Line 6:

     For each line that is read, expr increments the value of the num vari-
     able by the number of arguments in the input line ($#).

     The commands in lines 5 and 6 are repeated until the last input line
     has been read in by the read command. The echo command then outputs
     the value of the num variable, which is the total number of arguments
     read (total number of words).

NOTES
     set exists both as an external command (/usr/bin/sh) and as a built-in
     shell command, both in the Korn shell ksh(1) as well as in the Bourne
     shell sh(1). The shell generates a new process to execute /usr/bin/sh.

     Some differences in behavior may occur when using set, depending on
     which shell is being used. The possible differences are not described
     specifically.

SEE ALSO
     env(1), getopt(1), ksh(1), sh(1).









Page 8                       Reliant UNIX 5.44                Printed 11/98

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