I’m trying to figure out why the bail-on-error behavior -e
does not kick in if the failing block is in a conditional chain:
#!/bin/bash
set -e
{ echo "First"
ls blat
echo "(this should not print)"
}
Prints out:
First
ls: cannot access 'blat': No such file or directory
which is correct.
Whereas the following:
#!/bin/bash
set -e
{ echo "First"
ls blat
echo "(this should not print)"
} || echo "Encountered an error"
Prints out:
First
ls: cannot access 'blat': No such file or directory
(this should not print)
I expect Encountered an error
to be printed instead of this should not print
Can anybody explain to me the reason for the discrepancy?