Linux Command Reference ยท Shell Builtins
Bash set command
Reference for the Bash set builtin: errexit, nounset, pipefail, xtrace, noglob, positional parameters with -- and how to list variables and options.
set does three different things depending on how you call it. With options (set -e, set +x) it switches shell behavior on and off. With -- or plain words (set -- a b c) it replaces the positional parameters $1, $2, and so on. With no arguments it prints every variable and function the shell knows.
The most common use is the "strict mode" header of scripts: set -euo pipefail, which turns silent mistakes into immediate failures.
set [-abefhkmnptuvxBCEHPT] [-o OPTION-NAME] [--] [ARG ...] set +o
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
set [-abefhkmnptuvxBCEHPT] [+abefhkmnptuvxBCEHPT] [-o name] [+o name] [--] [arg ...]
| Name | Description |
|---|---|
-e, -o errexit |
Exit when a simple command fails (with exceptions: conditions, &&/` |
-u, -o nounset |
Treat expansion of an unset variable as an error. |
-o pipefail |
The exit status of a pipeline is the last non-zero status of any stage. |
-x, -o xtrace |
Print each command, prefixed with PS4 (default + ), before running it. |
-v, -o verbose |
Print input lines as they are read. |
-f, -o noglob |
Disable pathname expansion (*, ?, [...]). |
-C, -o noclobber |
> refuses to overwrite an existing file (use `> |
-n, -o noexec |
Read commands and check syntax without running them. |
-a, -o allexport |
Export every variable that is assigned afterwards. |
-B, -o braceexpand |
Brace expansion (on by default). |
-H, -o histexpand |
! history expansion (on by default in interactive shells). |
-o vi, -o emacs |
Line-editing style for the command line. |
+option |
The same letters with + turn the option off. |
-- |
End of options; the remaining words become the positional parameters. |
| no arguments | Print all shell variables and functions. |
set +o |
Print the current option settings as commands that can recreate them. |
Returns: 0 on success; non-zero for an invalid option or option name.
Errors and edge cases: in a non-interactive shell, set -u makes the shell exit on an unset variable (with status 127 in Bash 5.0, 1 in some other shells). set -e is silently disabled inside functions that are called as part of a condition (if myfunc), and a failure in the middle of a pipeline does not trigger it unless pipefail is set.
Usage notes
- Strict mode.
set -euo pipefailis common, but understand each part:-ehas surprising exceptions,-urejects${arr[@]}on an empty array in Bash before 4.4, andpipefailmakesgrep -qin a pipeline fail withSIGPIPE-related status. Handle expected failures explicitly with|| trueorif. - Debugging.
set -xshows what actually runs after expansion. SetPS4='+ ${BASH_SOURCE}:${LINENO}: 'to see file and line, and turn it off withset +xonce you have the answer. Runbash -x script.shto trace without editing the file. set --is the clean way to reset arguments, and it protects against values starting with a dash (set -- "$value"). Without--, a value like-ewould be read as an option.- No arguments prints everything, including functions, so pipe it through
lessorgrep.setoutput differs fromenv/export -p, which list only exported variables. - Options are scoped to the shell. A subshell or
bash -cstarts with defaults; options set in a script do not leak to its caller. A function sees the options of the caller. - Shell-specific options live under
shopt(nullglob,extglob,globstar);set -oholds the POSIX-style ones. allexport.set -a; source .env; set +aexports every assignment made while sourcing a settings file (source).- Interactive vs scripts.
set -ein your login shell will close the terminal on the first failed command. Keep it out of~/.bashrc.
Related
shopt,export,unset,trap,declare- Lessons: Exit Codes and Errors, Script Debugging, Arguments