4 ms·
That's not entirely what is happening there. The shell doesn't exit because the [[ "$BASH_SOURCE" == "$0" ]] condition is false. Its status is checked, so it do
by hvdijk 5y ago
That's not entirely what is happening there. The shell doesn't exit because the [[ "$BASH_SOURCE" == "$0" ]] condition is false. Its status is checked, so it doesn't cause the shell to exit. What's happening instead is that the exit status of the "source" command is that of the last command executed in the sourced file, and its status is not checked, so the failure there does cause the shell to exit under -e. Add a command that's just "true" or ":" or "echo debug" to foo.sh and you'll see a change in behaviour.
It's probably best to ensure that sourcing a source-able file results in a 0 return status if sourcing succeeded even under +e, since the caller may be using something along the lines of source file || { echo "failed to source file" >&2; exit 1; }. And sure, using if-then-fi rather than && can be a perfectly good way of achieving that.