Commands in a shell script are executed from top to bottom. If some commands
fail, the execution continues to the next command. For example:
$ cat my script.sh
#!/bin/bash
echo "Script started"
ls foo
echo "Script ended successfully"
$ ./my_script.sh
Script started
ls: cannot access 'foo': No such file or directory Script ended successfully
Failing commands do not stop the execution of the script. This might be acceptable in some cases but usually, it is wanted to make sure commands are succeeded. Commands can be combined with && or can be checked the result of the previous commands with 'if statements but, this adds extra complexity to the script. There is a better way: 'set -e'
$ cat my script.sh
#!/bin/bash
# set -e # Stop on first error
echo "Script started"
ls foo
echo "Script ended successfully"
$ ./my_script.sh
Script started
ls: cannot access 'foo': No such file or directory