Backup/restore Bash options including errexit

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