1860
Comment: trap ERR too
|
5237
empty line between anchor and headline breaks BashFAQ list
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
`set -e` was an attempt to add "automatic error detection" to the shell. Its ''goal'' was to cause the shell to abort any time an error occurred, so you don't have to put `|| exit 1` after each important command. | `set -e` was an attempt to add "automatic error detection" to the shell. Its goal was to cause the shell to abort any time an error occurred, so you don't have to put `|| exit 1` after each important command. |
Line 5: | Line 5: |
That goal is non-trivial, because many commands are ''supposed'' to return non-zero. For example, | That goal is non-trivial, because many commands intentionally return non-zero. For example, |
Line 8: | Line 8: |
if [ -d /foo ]; then ... else ... fi |
if [ -d /foo ]; then ...; else ...; fi |
Line 14: | Line 10: |
Clearly we don't want to abort when `[ -d /foo ]` returns non-zero (because the directory does not exist) -- our script wants to handle that in the `else` part. So the implementors decided to make a bunch of special rules, like "commands that are part of an `if` test are immune", or "commands in a pipeline, other than the last one, are immune". |
Clearly we don't want to abort when the conditional, `[ -d /foo ]`, returns non-zero (because the directory does not exist) -- our script wants to handle that in the `else` part. So the implementors decided to make a bunch of special rules, like "commands that are part of an `if` test are immune", or "commands in a pipeline, other than the last one, are immune". |
Line 22: | Line 17: |
#!/bin/bash | #!/usr/bin/env bash |
Line 28: | Line 23: |
''Exercise 2: why does ''this'' one sometimes appear to work? In which versions of bash does it work, and in which versions does it fail?'' |
''Exercise 2: why does '''this''' one sometimes appear to work? In which versions of bash does it work, and in which versions does it fail?'' |
Line 32: | Line 26: |
#!/bin/bash | #!/usr/bin/env bash |
Line 38: | Line 32: |
''Exercise 3: why aren't these two scripts identical?'' | |
Line 39: | Line 34: |
([[/Answers|Answers]]) | {{{#!highlight bash #!/usr/bin/env bash set -e test -d nosuchdir && echo no dir echo survived }}} {{{#!highlight bash #!/usr/bin/env bash set -e f() { test -d nosuchdir && echo no dir; } f echo survived }}} ''Exercise 4: why aren't '''these''' two scripts identical?'' |
Line 41: | Line 49: |
{{{#!highlight bash set -e f() { test -d nosuchdir && echo no dir; } f echo survived }}} {{{#!highlight bash set -e f() { if test -d nosuchdir; then echo no dir; fi; } f echo survived }}} ''Exercise 5: under what conditions will this fail?'' {{{#!highlight bash set -e read -r foo < configfile }}} ([[BashFAQ/105/Answers|Answers]]) Even if you use `expr(1)` (which we ''do not'' recommend -- use [[ArithmeticExpression|arithmetic expressions]] instead), you still run into the same problem: {{{#!highlight bash set -e foo=$(expr 1 - 1) # The following command will not be executed: echo survived }}} Subshells from command substitution unset `set -e`, however (unless `inherit_errexit` is set with Bash 4.4): {{{#!highlight bash set -e foo=$(expr 1 - 1; true) # Will run: echo survived }}} Note that set -e is '''not''' unset for commands that are run asynchronously, for example with process substitution: {{{#!highlight bash set -e mapfile foo < <(true; echo foo) echo ${foo[-1]} # foo mapfile foo < <(false; echo foo) echo ${foo[-1]} # bash: foo: bad array subscript }}} Another pitfall associated with `set -e` occurs when you use commands that ''look like'' assignments but aren't, such as `export`, `declare`, `typeset` or `local`. {{{#!highlight bash set -e f() { local var=$(somecommand that fails); } f # will not exit g() { local var; var=$(somecommand that fails); } g # will exit }}} In function `f`, the exit status of `somecommand` is discarded. It won't trigger the `set -e` because the exit status of `local` masks it (the assignment to the variable succeeds, so `local` returns status 0). In function `g`, the `set -e` is triggered because it uses a ''real'' assignment which returns the exit status of `somecommand`. Using [[ProcessSubstitution|Process substitution]], the exit code is also discarded as it is not visible from the main script: {{{#!highlight bash set -e cat <(somecommand that fails) echo survived }}} Using a pipe makes no difference, as only the ''rightmost'' process is considered: {{{#!highlight bash set -e somecommand that fails | cat - echo survived }}} `set -o pipefail` is a workaround by returning the exit code of the ''first'' failed process: {{{#!highlight bash set -e -o pipefail failcmd1 | failcmd2 | cat - # The following command will not be executed: echo survived }}} though with pipefail in effect, code like this will sometimes cause an error, depending on whether the output of somecmd exceeds the size of the pipe buffer or not: {{{#!highlight bash set -e -o pipefail somecmd | head -n1 # The following command will sometimes be executed, depending on how much output somecmd writes: echo survived }}} |
|
Line 42: | Line 137: |
rking's personal recommendation is to go ahead and use `set -e`, but beware of possible gotchas. It has useful semantics, so to exclude it from the toolbox is to give into FUD. geirha's personal recommendation is to handle errors properly and not rely on the unreliable `set -e`. |
Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected?
set -e was an attempt to add "automatic error detection" to the shell. Its goal was to cause the shell to abort any time an error occurred, so you don't have to put || exit 1 after each important command.
That goal is non-trivial, because many commands intentionally return non-zero. For example,
if [ -d /foo ]; then ...; else ...; fi
Clearly we don't want to abort when the conditional, [ -d /foo ], returns non-zero (because the directory does not exist) -- our script wants to handle that in the else part. So the implementors decided to make a bunch of special rules, like "commands that are part of an if test are immune", or "commands in a pipeline, other than the last one, are immune".
These rules are extremely convoluted, and they still fail to catch even some remarkably simple cases. Even worse, the rules change from one Bash version to another, as Bash attempts to track the extremely slippery POSIX definition of this "feature". When a SubShell is involved, it gets worse still -- the behavior changes depending on whether Bash is invoked in POSIX mode. Another wiki has a page that covers this in more detail. Be sure to check the caveats.
Exercise for the reader: why doesn't this example print anything?
Exercise 2: why does this one sometimes appear to work? In which versions of bash does it work, and in which versions does it fail?
Exercise 3: why aren't these two scripts identical?
Exercise 4: why aren't these two scripts identical?
Exercise 5: under what conditions will this fail?
(Answers)
Even if you use expr(1) (which we do not recommend -- use arithmetic expressions instead), you still run into the same problem:
Subshells from command substitution unset set -e, however (unless inherit_errexit is set with Bash 4.4):
Note that set -e is not unset for commands that are run asynchronously, for example with process substitution:
Another pitfall associated with set -e occurs when you use commands that look like assignments but aren't, such as export, declare, typeset or local.
In function f, the exit status of somecommand is discarded. It won't trigger the set -e because the exit status of local masks it (the assignment to the variable succeeds, so local returns status 0). In function g, the set -e is triggered because it uses a real assignment which returns the exit status of somecommand.
Using Process substitution, the exit code is also discarded as it is not visible from the main script:
Using a pipe makes no difference, as only the rightmost process is considered:
set -o pipefail is a workaround by returning the exit code of the first failed process:
though with pipefail in effect, code like this will sometimes cause an error, depending on whether the output of somecmd exceeds the size of the pipe buffer or not:
GreyCat's personal recommendation is simple: don't use set -e. Add your own error checking instead.
rking's personal recommendation is to go ahead and use set -e, but beware of possible gotchas. It has useful semantics, so to exclude it from the toolbox is to give into FUD.
geirha's personal recommendation is to handle errors properly and not rely on the unreliable set -e.