If the command cd
in this script fails it can be a disaster:
cd $HOME/el-directorio
rm -rf algo
You could use an if to check but when a script has dozens of commands it's expensive and error prone to have to put in a check every time.
How to make bash script stop on any error?
This causes the script to stop if any command returns a non-zero exit code.
There are some exceptions:
To return bash to normal behavior:
The
set -e
can also be activated with a parameter when invoking bash:With the exit command the shell script is terminated at any time. For example, include the following in your code:
exit 1
1 indicates a general error. There are other more specific error indicators:
For your specific example, I would first check if the directory exists: