Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash shell options (set -o and shopt)

Reference for Bash shell options: the set -o list (errexit, noclobber, pipefail...) and shopt flags (extglob, globstar, nullglob...), with defaults and short examples.

Bash behavior is controlled by two sets of switches. The set options come from the POSIX world: each one has a long name for set -o NAME and often a single letter for set -e. The shopt options are Bash-specific and are changed with shopt -s NAME (set) and shopt -u NAME (unset). Both sets are per-shell: subshells inherit them, but changes made in a subshell do not flow back.

This page is a map of the options you are most likely to need, what each does, and its default. Use set -o or shopt with no arguments to list every option and its current state.

Terminal
set -o                 # list set options (on/off)
set -o noclobber       # turn on;   set +o noclobber turns it off
shopt                  # list shopt options
shopt -s extglob       # turn on;   shopt -u extglob turns it off
shopt -q nullglob      # test quietly: exit status says on or off

Try it

Bash Inspecting and changing 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 Options that change globbing and redirection

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] [-o OPTION]
set [+abefhkmnptuvxBCEHPT] [+o OPTION]
shopt [-pqsu] [-o] [OPTION...]

Commonly used set options:

Option Meaning
-e, errexit Exit when a command fails (with the usual exceptions). Off by default.
-u, nounset Unset variables are errors. Off.
pipefail Pipeline status is the last non-zero stage. Off.
-x, xtrace Print each command before running it. Off.
-v, verbose Print input lines as they are read. Off.
-n, noexec Read commands but do not run them (a syntax check). Off.
-f, noglob Disable pathname expansion. Off.
-C, noclobber > refuses to overwrite an existing file. Off.
-a, allexport Export every variable that is assigned. Off.
-b, notify Report background job completion immediately. Off.
-m, monitor Job control. On in interactive shells.
-H, histexpand ! history expansion. On in interactive shells.
-B, braceexpand {a,b} expansion. On.
-E, errtrace ERR trap is inherited by functions and subshells. Off.
-T, functrace DEBUG and RETURN traps are inherited. Off.
-h, hashall Remember command locations. On.
-P, physical cd resolves symlinks. Off.
emacs, vi Command-line editing mode.
ignoreeof Ctrl+D does not exit the shell. Off.
posix Follow the POSIX standard more closely. Off.

Commonly used shopt options:

Option Meaning Default
extglob Extended patterns ?(...), *(...), +(...), @(...), !(...). off
globstar ** matches files and directories recursively. off
nullglob A pattern with no match expands to nothing. off
failglob A pattern with no match is an error. off
dotglob * also matches names that start with a dot. off
nocaseglob Case-insensitive pathname matching. off
nocasematch Case-insensitive case and [[ == ]], =~. off
lastpipe The last pipeline stage runs in the current shell (scripts only). off
inherit_errexit Command substitutions inherit set -e. off
expand_aliases Aliases work. On interactive, off in scripts. varies
histappend Append to the history file at exit instead of overwriting. off
checkwinsize Update LINES and COLUMNS after each command. on (5.0+)
cdspell, autocd Fix small typos in cd; type a directory name to enter it. off
interactive_comments Allow # comments at the prompt. on
xpg_echo echo interprets backslash escapes. off

Returns: set returns 0 unless given an invalid option. shopt -s/-u return 0 on success, and shopt -q NAME returns 0 if the option is on and 1 if it is off, which makes it usable in conditions.

Errors and edge cases: shopt -s of a name that does not exist prints "invalid shell option name" and returns 1. Use shopt -o to work with the set -o names through shopt.

Usage notes

  • Two syntaxes, one idea. set -o noclobber and set -C are the same switch; shopt options have no letter forms.
  • Inspect before you change. $- shows active single-letter flags, $SHELLOPTS lists enabled set -o names, $BASHOPTS the enabled shopt names (both read-only).
  • Restore state. Capture old=$(shopt -p NAME) and eval "$old" later, or wrap the work in a subshell so the setting disappears with it.
  • Scripts vs. interactive. Several options differ by default (expand_aliases, monitor, histexpand). Do not rely on interactive defaults in scripts.
  • Set extglob before a command is parsed. Patterns are recognized at parse time, so shopt -s extglob on the same line (or inside the same { } block or function body) as the pattern is too late.
  • Environment. Setting SHELLOPTS=xtrace in the environment before starting Bash turns the option on for it and all child Bash processes, which is useful for tracing a whole build.
  • Combine with care. set -e with inherit_errexit and pipefail makes failures louder but also changes how conditionals behave; see strict mode.