We had recent experience with bash that even we found a solution, it continues to meander. How does bash evaluate the && expression in terms of return codes?
Running this script, which should fail, since myrandomcommand does not exist:
#!/bin/bash set -e echo "foo" myrandomcommand echo "bar"
The result is the expected:
~ > bash foo.sh foo foo.sh: line 6: myrandomcommand: command not found [exited with 127] ~ > echo $? 127
But slightly changing the code with the && expression:
#!/bin/bash set -e echo "foo" myrandomcommand && ls echo "bar"
The ls statement is not executed (since the first statement fails and does not evaluate the second statement), but the script behaves in a completely different way:
~ > bash foo.sh foo foo.sh: line 6: myrandomcommand: command not found bar
We found that using the expression between the brackets (myrandomcommand && ls) works as expected (for example, the first example), but I would like to know why.
jaume source share