Backup/restore Bash options including errexit

Sept. 1, 2018, 11:49 p.m.

errexit is not propagated into command substitutions, so you cannot do this:

set -e
OPTS=$(shopt -po)

set +e

eval "$OPTS"

but it is propagated into process substitutions.

set -e

# Backup restore commands into an array
declare -a OPTS
readarray -t OPTS < <(shopt -po)

set +e

# Restore options
declare cmd
for cmd in "${OPTS[@]}"; do
    eval "$cmd"
done

Check:

$  shopt -po errexit
set -o errexit