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?
if
worked that way but it never occurred to me that bash would treat&&
the same way. – Penthouse