I have a script that calls another script. When the child script fails, I'd like the parent to fail as well.
In the child script child_1.sh
, I have something like this:
if [ $SOME_BAD_CONDITION ] ; then
echo "Error message...."
exit 1
fi
In the parent, I have this:
#!/bin/bash
set -e
#...
bash ./child_1.sh
echo "continuing to next step..."
bash ./child_2.sh
bash ./child_3.sh
#...
I've set up my environment so that $SOME_BAD_CONDITION
will always happen, and the script exits as expected and the error message does print, but the parent script continues: the message "continuing..." is printed and the next script begins executing.
I thought that having set -e
would ensure that my parent script fails if any child script exists with a non-zero exit code, but that doesn't seem to be happening. I'm not sure what I got wrong here...
bash version: 4.2.25
UPDATE:
I tried echoing $?
:
bash ./child_1.sh
echo $?
echo "continuing to next step..."
The output looks like this:
Error message.... 0 continuing to next step...
Why doesn't the exit code from the child make it into the parent?
ANSWER: The original code snippet was incomplete. The exit 1
was inside a code block that was piped to tee. My attempt to post a clean, short code sample ignored this because I did not realize how significant it was (I'm still fairly new to bash scripting). See my posted answer for details.
#...
ellipsis afterset -e
? – Stéphane Chazelas Feb 25 '15 at 17:35child_1.sh
have atrap
onEXIT
? Did you try adding aecho "$?"
afterbash ./child_1.sh
? – Stéphane Chazelas Feb 25 '15 at 17:40set -eu
in the parent script, and call the child script with./child_1.sh || exit 1
. – Yvan Jul 18 '17 at 07:19