The set
command is used to turn on and off shell options such as tracking
or automatic exporting of commands. Its second function is to reset
the values of positional parameters ($1).
The set
command syntax for POSIX Shell is:
set [-aCefnuvx-] [+aCefnuvx-] [-o
option]... [+o option]... [arg]...
and the set
command syntax for Korn Shell is:
set [-aefhkmnostuvx] [+aefhkmnostuvx] [-o option]... [+o option]...
[arg]...
where arg specifies the positional
parameters to be reset, and option can
specify with a word the same meaning as the aCefhkmnostuvx
letters.
For example, you can turn the verbose option on in two ways:
or
Here is an example showing results before and after the verbose
option is set:
$ echo hello hello $ set -o verbose $ echo hello echo hello hello |
The set -o verbose
causes the shell to print each line as it is read, and then print
the output of that line.
A discussion of other options follows:
- -a
All subsequent parameters that are defined are automatically
exported.
- -C
Prevents existing files from being overwritten by
the shell's >
redirection operator. The >|
operator overrides this noclobber
option.
- -e
If the shell is noninteractive and if a command
fails, execute the ERR trap, if
set, and exit immediately. This mode is disabled while reading .profile.
- -f
Disables file name generation.
- -h
Each command whose name is an identifier
becomes a tracked alias when first encountered.
- -k
All parameter assignment arguments are placed in
the environment for a command to use, not just those that follow
the command name.
- -m
Background jobs will run in a separate process group
and a line will print upon completion. The exit status of background
jobs is reported in a completion message.
- -n
Read commands but do not execute them.
- -s
Sort the positional parameters.
- -t
Exit after reading and executing one command.
- -u
Treat unset parameters as an error when substituting.
- -v
Print shell input lines as they are read.
- -x
Print commands and their arguments as they are executed.
- -
Turns off -x
and -v flags
and stops examining arguments for flags.
- --
Do not change any of the flags. This is also useful
in setting $1 to a value beginning
with - . If no
arguments follow this option then the positional parameters are
unset.
Using +
rather than -
causes these flags to be turned off.
These flags are the same ones used to invoke the shell:
or
which causes the shell to create a tracked alias for every
command executed.
The POSIX and Korn Shells implement an option, -o,
that turns on the specified argument or option
(i.e., set -o option).
Many of these options correspond to the above letters that perform
the same function without using -o.
The following argument or option can
be one of the following option names:
- allexport
Same as -a.
- errexit
Same as -e.
- emacs
Puts you in an emacs
style inline editor for command entry.
- gmacs
Puts you in a gmacs
style inline editor for command entry.
- ignoreeof
The shell will not exit on end-of-file. The command
exit must be used.
- keyword
Same as -k
.
- markdirs
All directory names resulting from file name generation
have a trailing /
appended.
- monitor
Same as -m.
- noexec
Same as -n.
- noclobber
Same as -C
for POSIX Shell.
- noglob
Same as -f.
- nounset
Same as -u.
- verbose
Same as -v.
- trackall
Same as -h.
- vi
Puts you in insert mode of a vi-style
inline editor until you press the ESC
key. This puts you in a mode so you can move on the line. A Return
executes the line.
- viraw
Each character is processed as it is typed in vi
mode.
- xtrace
Same as -x.
If you want a listing of all the currently set options, type:
$ set -o Current option settings allexport off bgnice off emacs off errexit off gmacs off ignoreeof off interactive off keyword off markdirs off monitor off noexec off noglob off nounset off protected off restricted off trackall on verbose off vi on viraw off xtrace off $ |
without options. This could be a very lengthy list, but should
have some of these items listed.
You can use the set
command in other ways, as in:
$ set third first second $ echo $1 $2 $3 third first second $ set -s $ echo $1 $2 $3 first second third |
where set
places the three values into the appropriate positional parameters,
and then sorts them and places them in the parameters in sorted
order.