3 ms·
I'm not sure what this person did expect. That bash magically parsed the output and memory of running programs, and read the user's thoughts, to determine if th
by pcthrowaway 4y ago
I'm not sure what this person did expect. That bash magically parsed the output and memory of running programs, and read the user's thoughts, to determine if the state of the program indicates a condition that the user would consider an error?
set -e does exactly what you'd expect, arguably, with the exception of subshells and conditions.
And those rules are extremely simple to learn too. If you understand when a statement which might be composed of other statements would have an error, you can predict what set -e will do
- wodenokoto 4y agoDid you look at the examples? The first examples has me completely puzzled. #!/usr/bin/env bash set -e i=0 let i++ echo "i is $i"
- thayne 4y agoWould you find it less surprising if the let command behaved differently than most other commands, and didn't stop the program if it returned a non-zero exit code? Because, this example works exactly the way I would expect. The next one (using the double parentheses syntax) is more questionable, and I could see it going either way, and would either look it up in the manual, or put an `|| true` after it to be safe. The fact that it changed in a minor release is pretty bad though.
- wodenokoto 4y agoCome again, why is `i++` obviously an error when `i` is zero? I’d expect an arithmetic operation to fail if I, say, divide by 0. Not if I add 1 to zero! And just because behavior is documented doesn’t mean it’s not surprising or complicated.
- powersnail 4y agoNot your parent commenter. I'd expect let command not to return a numeric value at all, since bash uses the returned numeric value to indicate success/failure. But that admittedly doesn't have anything to do with the design of "set -e".
- thayne 4y agolet doesn't return the numeric value of the expression as an exit code: > If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise. Which allows you to do things like: while let --i; do some command; done or if let x > 4; then some command; fi
- bogantech 4y agoIt's explained here: https://unix.stackexchange.com/a/32251 https://unix.stackexchange.com/a/32251 From help let: Exit Status: If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.. Since var++ is post-increment, I guess the last argument does evaluate to zero. Subtle...