4 ms·
Another variant which I overlooked to often is if you have a construct which returns an error but does not exit the script later move it in a function and now i
by Calzifer 3y ago
Another variant which I overlooked to often is if you have a construct which returns an error but does not exit the script later move it in a function and now it does exit the script.
set -e
[[ 0 == 1 ]] && true
echo Status: $?
will print "Status: 1" but
set -e
func() {
[[ 0 == 1 ]] && true
}
func
echo Status: $?
will print nothing but exit after func returns.
- js2 3y agoThere's quite a few situations where it does NOT exit when you'd expect it to. This is one of those. What you're seeing there is a variant of the "Short-Circuiting Considerations" discussed here: http://redsymbol.net/articles/unofficial-bash-strict-mode/ http://redsymbol.net/articles/unofficial-bash-strict-mode/ Basically, when you short circuit, it will not cause the script to exit. However, the short circuit still affects the return value of the function, or the script itself if it's the last line of the script. So your short circuit is causing the function to return non-zero, which then causes the script to exit.