3 ms·
I can't stand pipefail, and I'm ambivalent about set -e. It usually does what you want, which is sometimes worse than never doing what you want. It's usually
by aidenn0 6y ago
I can't stand pipefail, and I'm ambivalent about set -e. It usually does what you want, which is sometimes worse than never doing what you want. It's usually fairly straightforward to test for failure at the toplevel manually with something like "|| die 'useful message'"
What I would really like is if set -e would return from a function with an error (and if you wrap your entire function body in a subshell that's how it works). There are still some gotchas though: "false && true" will not cause an error exit, nor will "if someFunction; then ..." exit if someFunction encounters an error, which is a problem if you have e.g.:
someFunction() {
cd /some/path
rm -rf *
}
And expect "set -e" to prevent the remove command from running in the event of the CD failure.
Most shell scripts are short enough that you will notice the missing die in something like
foo || die
bar
baz || die
And tools like shellcheck will flag dangerous commands that are missing an error check.
So, given a fairly short toplevel, set -e has minimal advantages, and it can't be trusted to work inside a function. Sounds pretty questionable to me.
- graton 6y agoIsn't that what set -E is for, when used with set -e? So that an error in the function will also cause the script to terminate. From bash manpage: -E If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR trap is normally not inherited in such cases.
- aidenn0 6y agoNope, the -E has to do with the "trap" command. This example happily echoes "hello" to stdout: #!/bin/bash set -euE -o pipefail FOO=/this/does/not/exist foo() { cd "$FOO" echo hello } if foo; then true; fi
- graton 6y agoWell in that case it appears to be because of using "if foo" But that might have been your point in the first place :) I wasn't aware of the change in behavior with an 'if' statement. As a note, trap ERR had the same behavior as 'set -e' from my quick check. This dies: #!/bin/bash set -euE -o pipefail FOO=/this/does/not/exist foo() { cd "$FOO" echo hello } foo echo We do not reach here
- cellularmitosis 6y ago> I can't stand pipefail Can you elaborate on this? I'm finding it difficult to think up a case where I would want a script to continue executing if a command in the middle of a pipeline failed.
- aidenn0 6y agofoo | head -n20 foo may or may not return an error due to the pipe closing, and it's not necessarily consistent each time, particularly if the output of foo is very close to what head will consume.