NAME
set — set/unset
options and positional parameters
SYNOPSIS
set |
[-aCefnuvx] [argument
...] |
DESCRIPTION
set [+aCefnuvx] [argument . . . ]
set −− [argument . . . ] Obsolescent
version: set − [argument . . . ] If no
options or arguments are specified, set shall write
the names and values of all shell variables in the collation sequence of the
current locale. Each name shall start on a separate line, using the format:
"%s=%s\n", <name>, <value> The value string shall be
written with appropriate quoting so that it is suitable for re-input to the
shell, (re)setting, as far as possible, the variables that are currently
set. Readonly variables cannot be reset. See the
description of shell quoting in 3.2. When options are specified, they shall
set or unset attributes of the shell, as described
below. When arguments are specified, they shall cause positional parameters
to be set or unset, as described below.
Setting/unsetting attributes
and positional parameters are not necessarily related
actions, but they can be combined in a single invocation of
set. The set utility shall
conform to the utility argument syntax guidelines described in 2.10.2,
except that options can be specified with either a leading hyphen (meaning
enable the option) or plus-sign (meaning disable it). The implementation
shall support the options in the following list in both their hyphen and
plus-sign forms. These options can also be specified as options to sh; see
sh(1) . −a When this option is on, the export
attribute shall be set for each variable to which an
assignment is performed. (See 3.1.15.) If the assignment precedes a utility
name in a command, the export attributes shall not persist in the current
execution environment after the utility completes, with the exception that
preceding one of the special built-in utilities shall cause the export
attribute to persist after the built-in has completed. If the assignment
does not precede a utility name in the command, or if the assignment is a
result of the operation of the getopts or read utilities (see
getopts(1)
and read(1) ), the export attribute shall persist until the
variable is unset. −C (Uppercase C.) Prevent existing files from
being overwritten by the shell’s > redirection operator (see
3.7.2); the >| redirection operator shall override this
‘‘noclobber’’ option for an individual file.
−e When this option is on, if a simple command fails for any of the
reasons listed in 3.8.1 or returns an exit status value >0, and is not
part of the compound list following a while, until, or if keyword, and is
not a part of an AND
or OR list, and is not a pipeline preceded by the ! reserved word, then the
shell immediately shall exit. −f The shell shall disable pathname
expansion. −n The shell shall read commands but not execute them;
this can be used to check for shell script syntax errors. An interactive
shell may ignore this option. −u The shell shall write a message to
standard error when it tries to expand a variable that is not
set and immediately exit. An interactive shell shall
not exit. −v The shell shall write its input to standard error as it
is read. −x The shell shall write to standard error a trace for each
command after it expands the command and before it executes it. The default
for all these options is off (unset) unless the shell was invoked with them
on (see sh in sh(1) ). All the positional parameters shall
be unset before any new values are assigned.
3.14 Special Built-in Utilities 255
The remaining arguments shall be assigned in order to the
positional parameters. The special parameter # shall be
set to reflect the number of positional parameters.
The special argument "--" immediately following the
set command name can be used to delimit the
arguments if the first argument begins with + or −, or to prevent
inadvertent listing of all shell variables when there are no arguments. The
command set − − without arguments
shall unset all positional parameters and set the
special parameter # to zero. In the obsolescent version, the
set command name followed by − with no other
arguments shall turn off the −v and −x options without
changing the positional parameters. The set command
name followed by − with other arguments shall turn off the −v
and −x options and assign the arguments to the positional parameters
in order.
EXIT STATUS
Zero.
RATIONALE
The set − − form is listed
specifically in the Synopsis even though this usage is implied by the
utility syntax guidelines. The explanation of this feature removes any
ambiguity about whether the set − −
form might be misinterpreted as being equivalent to
set without any options or arguments. The
functionality of this form has been adopted from the KornShell. In System V,
set − − only unsets parameters if
there is at least one argument; the only way to unset all parameters is to
use shift. Using the KornShell version should not affect System V scripts
because there should be no reason to deliberately issue it without
arguments; if it were issued as, say: set −
− "$@"
and there were in fact no arguments resulting from $@, unsetting
the parameters would be a no-op anyway. The set +
form in earlier drafts was omitted as being an unnecessary duplication of
set alone and not widespread historical practice.
The noclobber option was changed to −C from the
set -o noclobber option in previous drafts. The
set −o is used in the KornShell to accept
word-length option names, duplicating many of the single-letter names. The
noclobber option was changed to a single letter so that the historical $-
paradigm would not be broken; see 3.5.2. The following
set flags were intentionally omitted with the
following rationale: −h This flag is related to command name hashing,
which is not required for an implementation. It is primarily a performance
issue, which is outside the scope of this standard.
−k The −k flag was originally added by
Bourne to make it easier for users of prerelease versions of the shell. In
early versions of the Bourne shell the construct set
name=value, had to be used to assign values to shell variables. The problem
with −k is that the behavior affects parsing, virtually precluding
writing any compilers. To explain the behavior of −k, it is necessary
to describe the parsing algorithm, which is implementation defined. For
example, set -k; echo name=value and
set -k echo name=value behave differently. The
interaction with functions is even more complex. What is more, the −k
flag is never needed, since the command line could have been reordered.
−t The −t flag is hard to specify and almost never used. The
only known use could be done with here-documents. Moreover, the behavior
with ksh and sh differ. The man page says that it exits after reading and
executing one command. What is one command? If the input is date;date, sh
executes both date commands, ksh does only the first. Consideration was
given to rewriting set to simplify its confusing
syntax. A specific suggestion was that the unset utility should be used to
unset options instead of using the non-
getopt()
-able +option syntax. However, the conclusion was reached that people were
satisfied with the existing practice of using +option and there was no
compelling reason to modify such widespread existing practice. Examples:
Write out all variables and their values: set Set
$1, $2, and $3 and set $# to 3:
set c a b Turn on the −x and −v
options: set −xv Unset all positional
parameters: set − − Set $1 to the
value of x, even if x begins with − or +: set
− − "$x" Set the positional parameters to the
expansion of x, even if x expands with a leading − or +:
set − − $x
3.14 Special Built-in Utilities 257