3 ms·
> For reasons known only to the almighty gods, set -e that usually causes an exit on a failed command, doesn't work that way inside functions. I was somewhat a
by ptx 6y ago
> For reasons known only to the almighty gods, set -e that usually causes an exit on a failed command, doesn't work that way inside functions.
I was somewhat alarmed for a second there, but at least on bash (version 5.1.4) "set -e" definitely does work inside functions. It doesn't work in pipes though - you'll also need "set -o pipefail" for that.
Busybox also seems to include awk. Would that have been a slightly less terrible choice of implementation language, or even worse?
- gray_-_wolf 6y agoActual caveat is that the -e does not work in if statements. Compare: +$ cat x #!/bin/sh set -eu foo() { echo 1 false echo 2 } foo +$ ./x 1 +$ cat x #!/bin/sh set -eu foo() { echo 1 false echo 2 } foo ||: +$ ./x 1 2
- shakna 6y ago> Busybox also seems to include awk. Would that have been a slightly less terrible choice of implementation language, or even worse? Two parts to that - I don't know awk well enough. I would need to re-implement diff/patch in awk, or make heavy use of sh anyway. > I was somewhat alarmed for a second there, but at least on bash (version 5.1.4) "set -e" definitely does work inside functions. The number of _exceptions_ to set -e is actually quite a number: > If not interactive, exit immediately if any untested command fails. The exit status of a command is considered to be explicitly tested if the command is used to control an if, elif, while, or until; or if the command is the left hand operand of an ''&&'' or ''||'' operator. set -o pipefail only catches some of those, especially the while and ifs that will continue to standout to blow your foot off... But even more import to note: sh is not bash. We're talking about POSIX shell. In Busybox's case, it is based on ash. Which doesn't actually have pipefail to begin with.