← Full reference

set and shopt

bash modes — set -euo pipefail, -x for debugging, noclobber, set -- and shopt options for globs

set and shopt are builtins that turn the shell's own modes on and off. set is used for the scripts' "strict mode" (set -euo pipefail), command tracing (set -x) and positional arguments (set -- …). shopt controls how * globs behave: an empty result instead of the pattern itself, recursive **, extended globs. Help — help set and help shopt.

Syntax

set -<letter>     # enable an option (minus)
set +<letter>     # disable an option (plus)
set -o name       # enable an option by its long name; set +o name — disable
set -o            # show all options and their state
set -- a b c      # replace positional arguments $1 $2 $3

shopt -s name     # enable a shopt option (set)
shopt -u name     # disable (unset)
shopt name        # show the state

Options last until the end of the current shell (script). They can also go into the shebang: #!/bin/bash -eu. The current short options are in $-.

set options

ShortLong (set -o …)What it does
-eerrexitexit the script as soon as a command returns a non-zero code (there are exceptions — see below)
-unounsetreferencing an unset variable is an error and exits
pipefailthe pipeline's code is the code of the last failed command, not just of the last command
-xxtraceprint each command before running it (to stderr, prefixed with $PS4, + by default)
-vverboseprint script lines as they are read, before substitutions
-Cnoclobber> doesn't overwrite an existing file; to force — >|
-fnoglobdon't expand *, ?, […] patterns
-nnoexeconly check syntax, run nothing (bash -n script.sh)
-aallexportautomatically export all new variables
-Eerrtracethe trap … ERR trap also fires inside functions
-TfunctraceDEBUG and RETURN traps fire inside functions
-bnotifyreport finished background jobs immediately
-mmonitorjob control (fg, bg); on in an interactive shell
-hhashallremember the paths of found commands (on by default)
-Bbraceexpandexpand {a,b} and {1..5} (on by default)
posixPOSIX compatibility mode
vi / emacscommand-line editing mode

Positional arguments: set --

CommandWhat it does
set -- a "b c" d$1=a, $2="b c", $3=d, $#=3
set --clear all arguments
set -- "$@" newappend an argument to the end
set -- $stringsplit a string by $IFS into arguments (deliberately unquoted)

shopt options

OptionWhat it does
nullgloba glob with no matches turns into nothing instead of staying the string *.txt
failgloba glob with no matches is a no match error, the command doesn't run
globstar** matches files and directories at any depth: **/*.log
dotglob* includes hidden files (names starting with a dot), except . and ..
nocaseglobcase-insensitive globs
extglobextended globs: !(…), @(…), +(…), *(…), ?(…)
nocasematch[[ == ]] and case are case-insensitive
lastpipethe last command of a pipeline runs in the current shell (works in scripts)
inherit_errexit$(…) inherits set -e
expand_aliasesexpand aliases in a script (by default only in an interactive shell)
login_shellread-only: on if the shell is a login shell

shopt flags: -s enable, -u disable, -q check quietly (code 0 — enabled), -p print as commands, -o work with set -o options.

Extended globs (extglob)

PatternMatches
!(pattern)everything that does not match the pattern: rm !(*.conf)
@(a|b)exactly one of the alternatives: *.@(jpg|png)
?(a)zero or one time
*(a)zero or more times
+(a)one or more times

Examples

#!/bin/bash
# "strict mode" at the top of a script
set -euo pipefail

# debug part of a script: turn tracing on and back off
set -x
cp /etc/hostname /tmp/
set +x

# a helpful trace prefix: the line number
PS4='+ ${LINENO}: '
set -x

# run a script with tracing without changing it
bash -x ./script.sh
# only check the syntax
bash -n ./script.sh

# don't clobber an existing file with an accidental >
set -o noclobber
echo data > /tmp/out.txt
echo new > /tmp/out.txt     # error: cannot overwrite existing file
echo new >| /tmp/out.txt    # explicit overwrite

# a loop over files that won't run at all if there are no files
shopt -s nullglob
for f in /var/log/*.gz; do
    echo "archive: $f"
done

# all .log files at any depth
shopt -s globstar
ls /var/log/**/*.log

# all files except .conf (enable extglob on a separate line before using it)
shopt -s extglob
ls /etc/!(*.conf)

# split a string into fields and refer to them as $1, $2
set -- $(date '+%Y %m %d')
echo "year $1, month $2"

# an unset variable under set -u — with a default value
echo "${DEBUG:-0}"

Pitfalls

  • set -e doesn't fire everywhere. An error is ignored in an if/while condition, to the left of && and ||, after !, and in any pipeline command except the last. A function called as f || … runs to the end even if commands inside it fail.
  • $(…) under set -e doesn't inherit -e. x=$(false; echo ok) gives x=ok. Enable shopt -s inherit_errexit.
  • local x=$(cmd) and echo "$(cmd)" hide cmd's error — the code comes from local or echo. A separate assignment x=$(cmd) under set -e does exit the script.
  • ((i++)) with i=0 under set -e exits the script — the exit code is 1. Write i=$((i + 1)).
  • Without pipefail an error at the start of a pipeline is lost. false | true returns 0. With set -o pipefail — 1.
  • set -u and empty arrays. "${arr[@]}" on a declared empty array works (bash 4.4+), but ${#arr[@]} on an undeclared array is an unbound variable error. For variables that may not exist, write ${VAR:-}.
  • nullglob changes how commands with an empty glob behave. ls *.txt with no matches turns into ls and shows the whole directory. Enable nullglob in scripts for loops, not globally in ~/.bashrc.
  • extglob must be enabled on a separate line before the pattern. Bash parses the whole line before running it, so shopt -s extglob; ls !(*.log) on one line gives syntax error near unexpected token '('.
  • Aliases don't work in scripts without shopt -s expand_aliases: the command gives command not found. In scripts write functions instead of aliases.

See also

exit-codes · variables · functions · redirection · shellcheck · env