Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
mywiki.wooledge.org
Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
1–10 of 114 posts
Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#2Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#3If you are wondering what in the world I'm talking about and I've missed your question entirely, sorry about that. My feet are tired.
Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#4Also noteworthy, this post of mine with authoritative text from GNU docs. Read "(Un)Portable Shell Programming" https://news.ycombinator.com/item?id=31678176
Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#5All set -e does is halt further execution of your script if any line exits above 0. There is really nothing bash can do about this. It can't perform a psychological evaluation on why a program is not giving the expected output. And most of the time, when something fails, if it were made by a less than serious programmer (like myself) they don't bother to exit on error correctly with a code above zero. But if you are…
Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#6All set -e does is halt further execution of your script if any line exits above 0. There is really nothing bash can do about this. It can't perform a psychological evaluation on why a program is not giving the expected output. And most of the time, when something fails, if it were made by a less than serious programmer (like myself) they don't bother to exit on error correctly with a code above zero. But if you are…
Also, error messages to stderr, please, and bonus points for not trying to emit cute ansi color codes unconditionally
Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#7set -e comes from the POSIX shell language, descended from the Bourne Shell.
The examples you're using use obscure Bash features. For instance let turns an arithmetic result into a termination status, where 0 is fail (opposite to the POSIX convention and all).
set -e works to the extent that your commands have a sane termination status, which they generally do if they are standard built-ins or well-behaved utilities.
One Bash feature improves the effectiveness of set -e (or exit status testing in general). In a command pipe:
a | b | .. | z
the termination status is obtained from z. That's standard. If z indicates success, the pipeline is successful no matter how a through y terminate. Bash has a "pipefail" option to help with this.Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#8All set -e does is halt further execution of your script if any line exits above 0. There is really nothing bash can do about this. It can't perform a psychological evaluation on why a program is not giving the expected output. And most of the time, when something fails, if it were made by a less than serious programmer (like myself) they don't bother to exit on error correctly with a code above zero. But if you are…
This is the expectation, and the article is about why it's not that simple.
Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#9Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)
#10All set -e does is halt further execution of your script if any line exits above 0. There is really nothing bash can do about this. It can't perform a psychological evaluation on why a program is not giving the expected output. And most of the time, when something fails, if it were made by a less than serious programmer (like myself) they don't bother to exit on error correctly with a code above zero. But if you are…