Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
set [-abefhkmnptuvxBCEHPT] [-o OPTION-NAME] [--] [ARG ...]
set +o

Try it

Bash Strict mode options

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.

Bash Positional parameters and listing options

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

Terminal
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 pipefail is common, but understand each part: -e has surprising exceptions, -u rejects ${arr[@]} on an empty array in Bash before 4.4, and pipefail makes grep -q in a pipeline fail with SIGPIPE-related status. Handle expected failures explicitly with || true or if.
  • Debugging. set -x shows what actually runs after expansion. Set PS4='+ ${BASH_SOURCE}:${LINENO}: ' to see file and line, and turn it off with set +x once you have the answer. Run bash -x script.sh to 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 -e would be read as an option.
  • No arguments prints everything, including functions, so pipe it through less or grep. set output differs from env/export -p, which list only exported variables.
  • Options are scoped to the shell. A subshell or bash -c starts 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 -o holds the POSIX-style ones.
  • allexport. set -a; source .env; set +a exports every assignment made while sourcing a settings file (source).
  • Interactive vs scripts. set -e in your login shell will close the terminal on the first failed command. Keep it out of ~/.bashrc.