4 ms·
Just adding `set -e` also exits the script when a subshell exits with non-zero error code. I'm not sure why I would leave `set -e` out in any shell script.
by wolletd 3y ago
Just adding `set -e` also exits the script when a subshell exits with non-zero error code. I'm not sure why I would leave `set -e` out in any shell script.
- porlw 3y agogrep has a bad interaction with set -e, since it (infuriatingly) exits with 1 if no lines are matched.
- wolletd 3y agoWell, otherwise `if grep "line" $file; then` wouldn't work, which in my opinion is the primary use case for grep in scripts. I'd still prefer a `grep <args> || true` over not having `set -e` for the whole file.
- dieulot 3y agoYou can `set +e` before and `set -e` after every such command. I indent those commands to make it look like a block and to make sure setting errexit again isn’t forgotten.
- usr1106 3y agoBut you probably still want an error if the input file does not exist. To handle grep correctly in a robust manner requires many lines of boilerplate, set +e, storing $?, set -e, distinguishing exit values 0, 1, and 2.
- deleted 3y ago[deleted]
- js2 3y agoI use `set -e` but it has its own quirks. A couple: An arithmetic expression that evaluates to zero will cause the script to exit. e.g this will exit: set -e i=0 (( i++ )) # exits Calling a function from a conditional prevents `set -e` from exiting. The following prints "hello\nworld\n": set -e main() { false # does not return nor exit echo hello } if main; then echo world; fi Practically speaking this means you need to explicitly check the return value of every command you run that you care about and guard against `set -e` in places you don't want the script to exit. So the value of `set -e` is limited. More at https://mywiki.wooledge.org/BashFAQ/105 https://mywiki.wooledge.org/BashFAQ/105