Work-arounds for being in "set -e" mode (Was: Re: why "shopt -p" sometim
From:
Stan Marsh
Subject:
Work-arounds for being in "set -e" mode (Was: Re: why "shopt -p" sometime return non-zero ?)
Date:
Thu, 25 Sep 2025 02:07:37 -0600
Duncan wrote:
>> `||:` also works - I use it sometimes in SBo scripts (all their templates
>> have `set -e`).
(Just out of curiosity, what is SBo?)
Martin wrote:
>Or a leading "!", like:
>! saved_shopts=$( shopt -p )
>Aside from a stylistic preference for a suffix rather than prefix, I wonder
>why this isn't more commonly suggested?
>PS: a prefix "!" does *not* cause errexit to be triggered when the command
>has a zero exit status, and it's been that way for a long time:
>$ set -e ; ! true ; ! false ; type ! ; echo $BASH_VERSION ; false ; echo
>Still here
>! is a shell keyword
>2.01.0(1)-release
Yes, this one has always struck me as odd. Not a bug, of course,
pretty much because, as you say, it's always been this way. I'm not
sure I really understand *why* it is not a bug (I've heard more than
one explanation from knowledgeable sources, but am still somewhat
unconvinced).
Another example is "select". If you do something like:
$ set -e
$ select var in one two three; do echo "var = $var"; done
$
And then hit ^D at the "#?" prompt, it will exit the "select"
statement with status 1 but will *not* abort the shell. So, you need
to append "|| exit" after the "done", to get that behavior. Again,
I'm sure there is a reason for this, but I remain unconvinced.
So, yes, "set -e" has quirks, but then again, shell is a quirky
language - pretty much every construct in the shell language has
quirks - things you need to test/figure out/become familiar with in
order to use the language effectively. And note that if this
*weren't* the case, there'd be no need for a certain web document (one
called something like "mywiki BASH FAQ", maintained by one of this
list's frequent posters).