Using the bash option set -e / set -o errexit and a trap to discontinue the execution of a script and notify the user if any step fails.
#!/bin/bash
fail_notice () {
echo "Command $BASH_COMMAND failed"
exit
}
trap fail_notice EXIT
set -o errexit #Exit the script if any untested command fails.
echo "before fail"
false # cause non zero exit code and trigger the trap
echo "after fail"
trap Exit # need to reset the Exit trap otherwise the fail_notice()
# will be triggered upon script completion (exit).