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
| Short | Long (set -o …) | What it does |
|---|---|---|
-e | errexit | exit the script as soon as a command returns a non-zero code (there are exceptions — see below) |
-u | nounset | referencing an unset variable is an error and exits |
pipefail | the pipeline's code is the code of the last failed command, not just of the last command | |
-x | xtrace | print each command before running it (to stderr, prefixed with $PS4, + by default) |
-v | verbose | print script lines as they are read, before substitutions |
-C | noclobber | > doesn't overwrite an existing file; to force — >| |
-f | noglob | don't expand *, ?, […] patterns |
-n | noexec | only check syntax, run nothing (bash -n script.sh) |
-a | allexport | automatically export all new variables |
-E | errtrace | the trap … ERR trap also fires inside functions |
-T | functrace | DEBUG and RETURN traps fire inside functions |
-b | notify | report finished background jobs immediately |
-m | monitor | job control (fg, bg); on in an interactive shell |
-h | hashall | remember the paths of found commands (on by default) |
-B | braceexpand | expand {a,b} and {1..5} (on by default) |
posix | POSIX compatibility mode | |
vi / emacs | command-line editing mode |
Positional arguments: set --
| Command | What it does |
|---|---|
set -- a "b c" d | $1=a, $2="b c", $3=d, $#=3 |
set -- | clear all arguments |
set -- "$@" new | append an argument to the end |
set -- $string | split a string by $IFS into arguments (deliberately unquoted) |
shopt options
| Option | What it does |
|---|---|
nullglob | a glob with no matches turns into nothing instead of staying the string *.txt |
failglob | a 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 .. |
nocaseglob | case-insensitive globs |
extglob | extended globs: !(…), @(…), +(…), *(…), ?(…) |
nocasematch | [[ == ]] and case are case-insensitive |
lastpipe | the last command of a pipeline runs in the current shell (works in scripts) |
inherit_errexit | $(…) inherits set -e |
expand_aliases | expand aliases in a script (by default only in an interactive shell) |
login_shell | read-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)
| Pattern | Matches |
|---|---|
!(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 -edoesn't fire everywhere. An error is ignored in anif/whilecondition, to the left of&&and||, after!, and in any pipeline command except the last. A function called asf || …runs to the end even if commands inside it fail.$(…)underset -edoesn't inherit-e.x=$(false; echo ok)givesx=ok. Enableshopt -s inherit_errexit.local x=$(cmd)andecho "$(cmd)"hidecmd's error — the code comes fromlocalorecho. A separate assignmentx=$(cmd)underset -edoes exit the script.((i++))withi=0underset -eexits the script — the exit code is 1. Writei=$((i + 1)).- Without
pipefailan error at the start of a pipeline is lost.false | truereturns 0. Withset -o pipefail— 1. set -uand empty arrays."${arr[@]}"on a declared empty array works (bash 4.4+), but${#arr[@]}on an undeclared array is anunbound variableerror. For variables that may not exist, write${VAR:-}.nullglobchanges how commands with an empty glob behave.ls *.txtwith no matches turns intolsand shows the whole directory. Enablenullglobin scripts for loops, not globally in~/.bashrc.extglobmust be enabled on a separate line before the pattern. Bash parses the whole line before running it, soshopt -s extglob; ls !(*.log)on one line givessyntax error near unexpected token '('.- Aliases don't work in scripts without
shopt -s expand_aliases: the command givescommand not found. In scripts write functions instead of aliases.
See also
exit-codes · variables · functions · redirection · shellcheck · env