# Set built-in

The set built-in sets shell options and positional parameters.

## Syntax

- `set [options] [operands]`

- `set -o`

- `set +o`

The set built-in requires that all options precede operands. Any command line arguments after the first operand are all treated as operands.

## Description

When executed without any command arguments, the built-in prints a list of all existing variables to the standard input in a form that can be reused as commands that will restore the variable definitions.

When `-o` is the only command argument, the built-in prints a list of shell options with their current settings. When `+o` is the only command argument, the built-in prints commands that can be reused to restore the current shell option settings.

In other cases, the built-in changes shell option settings and/or positional parameters.

## Options

When one or more options are specified, the built-in enables or disables the shell options. A normal hyphen-prefixed option enables a shell option. An option that is prefixed with a plus (`+`) instead of a hyphen disables a shell option. For example, options `-m`, `-o monitor`, and `--monitor` enable the monitor option and options `+m`, `+o monitor`, `++monitor` disable it.

The name of a long option is case-insensitive and may include irrelevant non-alphanumeric characters, which are ignored. For example, options `--le-comp-debug` and `--LeCompDebug` are equivalent. If `no` is prepended to the name of a long option, the meaning is reversed. For example, `--noallexport` is equivalent to `++allexport` and `++nonotify` to `--notify`.

An option can be specified in one of the following forms:

- a long option e.g. `--allexport`

- an `-o` option with a option name specified as the argument e.g. `-o allexport`

- a single-character option e.g. `-a`

Not all options can be specified as single-character options.

The available options are:

all-export (`-a`)

  When enabled, all variables are automatically exported when assigned.

brace-expand

  This option enables brace expansion.

case-glob

  (Enabled by default) When enabled, pattern matching is case-sensitive in pathname expansion.

clobber (`+C`)

  (Enabled by default) When enabled, the `>` redirection behaves the same as the `>|` redirection.

cur-async
cur-bg
cur-stop

  (Enabled by default) These options affect choice of the current job (cf. job ID).

dot-glob

  When enabled, periods at the beginning of filenames are not treated specially in pathname expansion.

emacs

  This option enables line-editing in the emacs mode.

empty-last-field

  When enabled, field splitting does not remove the last field even if it is empty.

err-exit (`-e`)

  When enabled, if a pipeline ends with a non-zero exit status, the shell immediately exits unless the following suppress condition is met:

  - the pipeline is a condition of an if command or while or until loop;

  - the pipeline is prefixed by `!`; or

  - the pipeline is a single compound command other than a subshell grouping.

err-return

  This option is like the err-exit option, but the return built-in is executed instead of the shell exiting on a non-zero exit status. Unlike err-exit, the suppress condition does not apply inside a function, subshell grouping, or script file.

exec (`+n`)

  (Enabled by default) Commands are actually executed only when this option is enabled. Otherwise, commands are just parsed and not executed. Disabling this option may be useful for syntax checking. In an interactive shell, this option is always assumed enabled.

extended-glob

  This option enables extension in pathname expansion.

for-local

  (Enabled by default) If a for loop is executed within a function, this option causes the iteration variable to be created as a local variable, even if the variable already exists globally. This option has no effect if the POSIXly-correct mode is active.

glob (`+f`)

  (Enabled by default) This option enables pathname expansion.

hash-on-def (`-h`)

  When a function is defined when this option is enabled, the shell immediately performs command path search for each command that appears in the function and caches the command’s full path.

hist-space

  When enabled, command lines that start with a whitespace are not saved in command history.

ignore-eof

  When enabled, an interactive shell does not exit when EOF (end of file) is input. This prevents the shell from exiting when you accidentally hit Ctrl-D.

le-always-rp
le-comp-debug
le-conv-meta
le-no-conv-meta
le-predict
le-predict-empty
le-prompt-sp
le-visible-bell
le-trim-right

  See shell options on line-editing.

mark-dirs

  When enabled, resulting directory names are suffixed by a slash in pathname expansion.

monitor (`-m`)

  This option enables job control. This option is enabled by default for an interactive shell.

notify (`-b`)

  When the status of a job changes when this option is enabled, the shell immediately notifies at any time. This option overrides the notify-le option.

notify-le

  This option is similar to the notify option, but the status change is notified only while the shell is waiting for input with line-editing.

null-glob

  When enabled, in pathname expansion, patterns that do not match any pathname are removed from the command line rather than left as is.

pipe-fail

  When enabled, the exit status of a pipeline is zero if and only if all the subcommands of the pipeline exit with an exit status of zero.

posixly-correct

  This option enables the POSIXly-correct mode.

trace-all

  (Enabled by default) When this option is disabled, the x-trace option is temporarily disabled while the shell is executing commands defined in the `COMMAND_NOT_FOUND_HANDLER`, `PROMPT_COMMAND`, or `YASH_AFTER_CD` variable.

unset (`+u`)

  (Enabled by default) When enabled, an undefined parameter is expanded to an empty string in parameter expansion and treated as zero in arithmetic expansion. When disabled, expansion of an undefined parameter results in an error.

verbose (`-v`)

  When enabled, the shell prints each command line to the standard error before parsing and executing it.

vi

  This option enables line-editing in the vi mode. This option is enabled by default in an interactive shell if the standard input and error are both terminals.

x-trace (`-x`)

  When enabled, the results of expansion are printed to the standard error for each simple command being executed. When printed, each line is prepended with an expansion result of the `PS4` variable. See also the trace-all option.

## Operands

If one or more operands are passed to the set built-in, current positional parameters are all removed and the operands are set as new positional parameters. If the `--` separator (cf. syntax of command arguments) is passed, the positional parameters are set even when there are no operands, in which case new positional parameters will be nothing.

## Exit status

The exit status of the set built-in is zero unless there is any error.

## Notes

The set built-in is a special built-in.

In the POSIX standard, available shell options are much limited. The standard does not define:

- long options such as `--allexport`,

- prepending `no` to negate an option,

- using uppercase letters and/or non-alphanumeric characters in option names

The options defined in the standard are:

- `-a`, `-o allexport`

- `-e`, `-o errexit`

- `-m`, `-o monitor`

- `-C`, `-o noclobber`

- `-n`, `-o noexec`

- `-f`, `-o noglob`

- `-b`, `-o notify`

- `-u`, `-o nounset`

- `-v`, `-o verbose`

- `-x`, `-o xtrace`

- `-h`

- `-o ignoreeof`

- `-o nolog`

- `-o vi`

Yash does not support the nolog option, which prevents function definitions from being added to command history.