Bash - stopping on failures
A good habit is to begin each bash script with:
set -e
(stop execution if a command returns with non-zero exit status)set -u
(stop execution if an undefined variable has been used, instead of treating the variable to have an empty value)
However, the set -e
commnd has some consequences. The script can end where we would not expect it:
We can avoid treating these cases as errors by surrounding the commands by set +e
and set -e
:
A function doing this arround a command can help to simplify the code:
It can be used as follows:
Written on February 12, 2016