Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

set - set or unset options and positional parameters

&pagelevel(4)&pagelevel

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...]


Format 1: set [+|-AabCefhkmnuvx][ -t][-o option...][ 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.
To access the tenth and subsequent command-line arguments directly you need to use the shift command.

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 single 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 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:

-A name

Assigns values to fields:
Deletes the value of the variable name and sequentially assigns it values from the argument list.

+A

If you use the plus sign +A, the values are not deleted before assignment.

-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 automatically.

-b

(background)

All background jobs are displayed. The following message is written to

standard error output:

"[%d]%c %s%s\n", <job-number>,<current>,<status>,<job-name>

<current>

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.

<job-number>

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

-b is reset.

-C

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

-C is reset.

-e

(exit) 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.

-f

(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:
Matching file names are substituted for the above patterns.

+f

Turns off option -f.

-h

(hash) This option changes the behavior of the current shell when you define shell

functions:
The commands you include in a shell function are entered in the hash table at the time of function definition (see hash).

-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 Example 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 environment of the command.

+k

Turns off option -k.

-m

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

-m is reset.

-n  

(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:
All commands are read, interpreted, and executed.

+n

-n is reset (except in interactive shells).

-o argument

argument may be one of the following option names:

allexport

Corresponds to -a

errexit

Corresponds to -e

bgnice

All background jobs are executed with low priority. This is the default presetting.

ignoreeof

The POSIX shell is not terminated at end-of-file. To terminate the shell, use the built-in command.

keywords

Corresponds to -k

markdirs

All directories which are created when the file name is generated are terminated by a backslash /.

monitor

Corresponds to -m

noclobber

No existing files are overwritten if output is redirected using >.

noexec

Corresponds to -n

noglob

Corresponds to -f

nolog

Function definitions are not stored in the History file.

nounset

Corresponds to -u

privileged

Corresponds to -p

verbose

Corresponds to -v

trackall

Corresponds to -h

vi

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.

viraw

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.

xtrace

corresponds to -x

-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

(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:
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

(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.
In contrast to the output with option -v, the -x option shows you how the shell has replaced metacharacters and shell variables.

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.

argument

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:
The positional parameters and the shell parameters $@, $* and $# are not modified.



Format 2: set -- [ argument...]


--

(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.

argument

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:
The positional parameters and the shell parameters $@, $* and $# are left unchanged.



Format 3: set - [ argument...]


-

Options -x and -v are reset.

argument

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:
The positional parameters are left unchanged.

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:

$ 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 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:

$ 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

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:

$ 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 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:
The sh built-in read reads a line from the standard input and assigns it to the variable line.

Line 5:
The sh 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 environment.

  • 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 variable 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).

See also

env, getopts, typeset