set -eo pipefail
commandThatFails || exitFunction
exitFunction
So this script is running the exitMethod twice... I thought set -e
exited immediately on any non zero exit code and set -o pipefail
made sure that during the pipeline any failure is the final exit status code not the most recent command?
Therefore I thought :
- commandThatFails
- Executes exitFunction
- set -o pipefail returns non zero exit code as the first command fails
- Non zero exit code is detected by set -e and exits immediately
In the docs it states:
The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command’s return status is being inverted with !. If a compound command other than a subshell returns a non-zero status because a command failed while -e was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits.
I thought that the exitfunction is the command following the final ||
so therefore would be counted and picked up and exited immediately.
I can resolve the issue with:
commandThatFails || { exitFunction; exit 1; }
but it doesn't seem like the more elegant way to handle this, any thoughts appreciated!