Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does bash not stop on error for failures in sequence of short-circuited commands?

I'm seeing some behavior that doesn't make sense to me when I run a bash script with the -e option that has multiple commands strung together with &&s and one of them fails. I would expect the script to stop on the failed command and return the exit status, but instead it just executes the rest of the script happily.

Here are examples that make sense to me:

$ false && true; echo $?
1

$ bash -xe -c "false && true"; echo $?
+ false
1

$ bash -xe -c "false; true"; echo $?
+ false
1

And here is the one that does not make sense to me:

$ bash -xe -c "false && true; true"; echo $?
+ false
+ true
0

This is where I don't understand what is going on. false && true returns status 1 so shouldn't the script stop executing and return status 1, like it does when the script is false; true?

While experimenting, I found that it works the way I would expect if I surround the chain of commands with parentheses:

$ bash -xe -c "(false && true); true"; echo $?
+ false
1

Can anybody give an explanation for this?

like image 625
crudcore Avatar asked Feb 12 '13 17:02

crudcore


1 Answers

The logic here is that your use of && already is error-checking. The same way bash doesn't treat a failure within an if condition as worth aborting, even with set -e.

When you wrap the commands in a parenthesis, you are actually running those commands within a subshell, so the script itself only sees the return of that subshell, ie: it isn't aware that && is involved at all, so it aborts as you expect.

like image 109
Will Palmer Avatar answered Nov 15 '22 05:11

Will Palmer