The POSIX shell built-in command set has three functions:
If no operand is specified, set writes on standard output the names and values of allshell 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 command, but in this case they govern the behavior of the subshell thus generated.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.
Syntax
Format 1: |
set [+|-AabCefhkmnuvx][ -t][-o option...][ argument...] |
Format 2: |
set -- [ argument...] |
Format 3: |
set - [ argument...] |
No operand specified set writes on standard output the names and values of all shell variables defined for the current shell. 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 parameters are set to the null string.
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 single minus sign at the beginning. You can use the command echo $- to find out which options have already been set with the set or sh command. Set options other than -n and -t can also be unset without the need to terminate the current shell. The following options are available:
Assigns values to fields:
If you use the plus sign +A, the values are not deleted before assignment.
Automatically exports all shell variables that you define or redefine in the current shell. -a not specified and not set previously:
Turns off option -a. Any shell variables defined earlier while the -a option was active will continue to be exported automatically.
(background) All background jobs are displayed. The following message is written to standard error output:
The + sign identifies the job that would be used as a default for the fg or bg utilities; this job can also be specified using the job_id %+ or %%. The - sign identifies the job that would become the default if the current default job were to exit; this job can also be specified using the job_id %-. All other jobs are represented by a blank character. Only one job can be identified with a + and one with a - at any given time.
A number that can be used to identify the process group to the wait, fg, bg and kill utilities. Using these utilities, the job can be identified by prefixing the job number with %.
-b is reset.
Prevents files from being overwritten when output is redirected with ">". If the output is redirected using ">|" then the noclobber option is overridden for the file in question.
-C is reset.
(exit) The current shell exits immediately if a command returns a non-zero exit status. -e not specified and not set previously:
Turns off option -e.
(file name) Disables the file name generation mechanism. The characters *, ? and [...] are not treated as metacharacters and expanded to generate all the matching file names. -f not specified and not set previously:
Turns off option -f.
(hash) This option changes the behavior of the current shell when you define shell functions: -h not specified and not set previously:
Turns off option -h.
(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 Example 1). -k not specified and not set previously:
Turns off option -k.
Background commands are executed in an individual process group. Command termination is reported in a line. The exit status of background jobs is acknowledged by a termination message. If the POSIX shell is inactive this option is automatically selected.
-m is reset.
(no execution) 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 the section “Processing commands using the POSIX shell”). The -n option can be used to check a shell script for syntax errors. The -n option is ignored in an interactive shell. -n not specified and not set previously:
-n is reset (except in interactive shells).
argument may be one of the following option names:
Corresponds to -a
Corresponds to -e
All background jobs are executed with low priority. This is the default presetting.
The POSIX shell is not terminated at end-of-file. To terminate the shell, use the built-in command.
Corresponds to -k
All directories which are created when the file name is generated are terminated by a backslash /.
Corresponds to -m
No existing files are overwritten if output is redirected using >.
Corresponds to -n
Corresponds to -f
Function definitions are not stored in the History file.
Corresponds to -u
Corresponds to -p
Corresponds to -v
Corresponds to -h
Depends on the value of the TERM variable. If TERM=BLOCK or if TERM is not set then the command assumes that a BS2000 block-mode terminal is in use and the argument vi is not supported. Any other value of TERM indicates a character-mode terminal. The argument vi changes the input mode to that of a vi type line editor until you press ESC. This switches you to command mode. The line sends a newline character.
Depends on the value of the TERM variable. If TERM=BLOCK or if TERM is not set then the command assumes that a BS2000 block-mode terminal is in use and the argument vi is not supported. Any other value of TERM indicates a character-mode terminal. If vi mode is active then the viraw argument causes each character to be processed as soon as it is typed.
corresponds to -x
(terminate) The current shell exits after executing one command, i.e. the command line that contains the set -t command. -t not specified:
(unset) Causes the current shell to issue an error message on encountering 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:
Turns off option -u.
(verbose) The current shell displays each subsequent input on standard error before executing the corresponding command. -v not specified and not set previously:
Turns off option -v.
(execute) The current shell interprets its input and writes it on the standard 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. Thus you can test a shell script as follows:
-x not specified and not set previously:
Turns off option -x.
Any string which is delimited by a space or a tab. The final argument is terminated by a command separator. You may enter any number of arguments provided that each is delimited by at least one space or tab. set assigns the first nine arguments to the positional parameters $1, $2, ... and $9. set also assigns all the specified arguments to the shell parameters $* and $@ and writes the total number of specified arguments to the shell parameter $#. If fewer than nine arguments are specified, empty strings are assigned to the remaining positional parameters. Use shift to address the tenth and other remaining call arguments directly. argument not specified: |
(Dashes) 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 positional parameters (see Example 3). set -- has no effect on any other options currently in effect.
Any string delimited by blanks or tabs. The last argument is terminated 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 parameters 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: |
Options -x and -v are reset.
set assigns the first nine arguments to the positional parameters $1, $2, ... and $9. If fewer than nine arguments are specified, empty strings are assigned to the remaining positional parameter. Use shift to address the tenth and other remaining call arguments directly. argument not specified: |
Locale
The following environment variables affect the execution of set: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
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:
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 subshell 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:
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.
|
Example 2
The shell script evaltest has the following contents: set -xv eval echo \$$# echo $0 The set command sets the -x and -v options in the subshell that executes this shell script. The shell script is then called as shown below:
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 output 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 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 6 num=`expr $num + $#` 7 done 8 echo $num Line 3: Line 5: In this example the -- option has two functions:
Line 6: 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). |
See also
env, getopts, typeset |