Live data from Hacker News

Why doesn't Bash’s ‘set -e’ do what I expected? (2021)

mywiki.wooledge.org

1–10 of 114 posts

Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)

#3
All 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 building a script collection, or python utility or even an binary, simply exit 1 if you encounter a general error. Then further up the chain in your shell scripts the error can be handled properly. There are more specific error codes you can use that might be helpful to your shell script.

If 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)

#4
post #2

Also noteworthy, this post of mine with authoritative text from GNU docs. Read "(Un)Portable Shell Programming" https://news.ycombinator.com/item?id=31678176

Or, as Larry Wall put it, "It is easier to port a shell than a shell script."

Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)

#5
post #3

All 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)

#6
post #5
post #3

All 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

CI tools: telling you who the asshole is, since 2001.

Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)

#7
Mainly, the reason why Bash's "set -e" doesn't do what you expected is this.

set -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)

#8
post #3

All 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…

> All set -e does is halt further execution of your script if any line exits above 0.

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)

#9
You don't have to be hyper-aware of false positives with "set -euo pipefail". False positives bring themselves to your attention during testing, while false negatives don't announce themselves at all. It's almost always preferable for your code to incorrectly fail and force you to stick "|| true" on the end, than to incorrectly succeed and let you miss the bug.

Re: Why doesn't Bash’s ‘set -e’ do what I expected? (2021)

#10
post #3

All 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…

The `let ++` example is pretty damning. I find Bash to be a poor and outdated shell that we’re stuck with for now. Its design is the best argument against LSD I know, it came out of the hippie drug days, and Bash reflects that with its madness.
Post reply on HN