7

I have read through many questions on various stack exchange sites and unix help forums on how to modify shell options and then restore them - the most comprehensive one I found on here is at How to "undo" a `set -x`?

The received wisdom seems to be to either save off the result of set +o or shopt -po and then eval it later to restore the previous settings.

However, in my own testing with bash 3.x and 4.x, the errexit option does not get saved correctly when doing command substitution.

Here is an example script to show the issue:

set -o errexit
set -o nounset
echo "LOCAL SETTINGS:"
set +o
OLDOPTS=$(set +o)
echo
echo "SAVED SETTINGS:"
echo "$OLDOPTS"

And output (I trimmed out some of the irrelevant variables):

LOCAL SETTINGS:
set -o errexit
set -o nounset

SAVED SETTINGS:
set +o errexit
set -o nounset

This seems extremely dangerous. Most scripts I write depend on errexit to halt execution if any commands fail. I just located a bug in one of my scripts caused by this, where the function that was supposed to restore errexit at the end wound up overriding it, setting it back to the default of off for the duration of the script.

What I'd like to be able to do is write functions that can set options as needed and then restore all the options properly before exiting. But it seems as if in the subshell invoked by the command substitution, errexit is not inherited.

I'm at a loss for how to save off the result of set +o without using command substitution or jumping through FIFO hoops. I can read from $SHELLOPTS but it is not writable or in eval-able format.

I know one alternative is to use a subshell function, but that introduces a lot of headaches for being able to log output as well as pass back multiple variables.

Probably related: https://stackoverflow.com/questions/29532904/bash-subshell-errexit-semantics (seems there is a workaround for bash 4.4 and up but I'd rather have a portable solution)

  • This doesn't restore bash shopt set options (like nullglob). – Isaac Sep 23 '18 at 19:03
6

What you're doing should work. But bash turns off the errexit option in command substitutions, so it preserves all the options except this one. This is specific to bash and specific to the errexit option. Bash does preserve errexit when running in POSIX mode. Since bash 4.4, bash also doesn't clear errexit in a command substitution if shopt -s inherit_errexit is in effect.

Since the option is turned off before any code runs inside the command substitution, you have to check it outside.

OLDOPTS=$(set +o)
case $- in
  *e*) OLDOPTS="$OLDOPTS; set -e";;
  *) OLDOPTS="$OLDOPTS; set +e";;
esac

If you don't like this complexity, use zsh instead.

setopt local_options
  • 3
    Note that bash4.4 now has local - (à la ash) as an equivalent to zsh's setopt localoptions (or what ksh88 does by default) – Stéphane Chazelas Oct 6 '17 at 6:06
  • See also shopt -s lastpipe; set +o | IFS= read -rd '' OLDOPTS || : (the two sets of options is another of bash's idiosyncrasies). – Stéphane Chazelas Oct 6 '17 at 6:15
  • Simpler: OLDOPTS="$(set +o); set -$-". – Isaac Oct 20 '18 at 16:19
1

errexit 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

Bash version:

$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
1

The simple solution is to append the errexit setting to OLDOPTS:

OLDOPTS="$(set +o)"
[ "${BASH_VERSION:+x}" ] && shopt -qo errexit && OLDOPTS+=";set -e" || true

Done.

  • See also [[ -o errexit ]] (ksh/bash/zsh) and [ -o errexit ] (bash/ksh/yash) – Stéphane Chazelas Nov 7 '18 at 13:59
  • The shopt is a valid command for bash, there is no meaningful reason to avoid it (I is only a matter of your personal preference of how answers should be written). Not a meaningful change. @StéphaneChazelas – Isaac Nov 9 '18 at 6:15
0

After trying old of the above on alpine 3.6, I've now taken the following much simpler approach:

OLDOPTS="$(set +o); set -${-//c}"
set -euf -o pipefail

... my stuff

# restore original options
set +vx; eval "${OLDOPTS}"

as per documentation, "$-" holds the list of currently active options. Seems to work great, am I missing anything?

  • @Isaac I use 'set +euf' because $- only contains the list of active options. i.e. when resetting I first deactivate everything and then reactivate only the list of previously active options (I choose 'euf' because those are the only options I usually modify - for general purpose use it probably should contain the full list of possible options). Good point about 'set +vx', I modified the example above accordingly – Erich Eichinger Oct 22 '18 at 2:36
  • that may work for errexit but not e.g. for bounds checking or globbing (set -uf) – Erich Eichinger Oct 22 '18 at 2:48
  • @Isaac I stand corrected. I don't know what I did wrong. Thanks a tons for your help! Also good to see that you ran into and solved the -c flag problem - I wish I had seen your comment earlier ;) – Erich Eichinger Nov 6 '18 at 21:07
  • I updated above solution based on @Isaac 's feedback – Erich Eichinger Nov 7 '18 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.