4 ms·
Did 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"
by wodenokoto 4y ago
Did 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...