Might be a basic question, but I'm not sure why logging would cause this exit function to not work as expected:
#!/bin/bash
function exitFunct
{
exit 1
}
exitFunct 2>&1 | tee -a /var/tmp/output.txt
echo "You should never see this"
But output is "You should never see this"
As man bash
explains,
Each command in a pipeline is executed as a separate process (i.e., in a subshell).
Therefore, the exit
in the function exits just the subshell that runs the function's part of the pipeline.
And also,
The return status of a pipeline is the exit status of the last command, unless the
pipefail
option is enabled.
Therefore, you can change the behaviour by prepending
set -eo pipefail
to the script (-e
makes your script stop on error). Nonetheless, note that using exit 0
wouldn't end it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With