Issue
I'm a bit confused here. My goal here is to have the bash script exit with a non-zero exit code when any of the commands within the script fails. Using the -e flag, I assumed this would be the case, even when using subshells. Below is a simplified example:
#!/bin/bash -e
(false)
echo $?
echo "Line reached!"
Here is the output when ran:
[$]>Tests/Exec/continuous-integration.sh
1
Line reached!
Bash version: 3.2.25 on CentOS
Solution
It appears as though this is related to your version of bash
. On machines that I have access to, bash version 3.1.17 and 3.2.39 exhibit this behaviour, bash 4.1.5 does not.
Although a bit ugly, a solution that works in both versions could be something like this:
#!/bin/bash -e
(false) || exit $?
echo $?
echo "Line reached!"
There are some notes in the bash source changelog which related to bugs with the set -e
option.
Answered By - Austin Phillips Answer Checked By - Katrina (WPSolving Volunteer)