To stop a bash script at first error use set -e
. To undo it use set +e
.
#!/bin/bash set +e ls -l /opt/non_existing_dir1 set -e echo "I will be printed" ls -l /opt/non_existing_dir2 echo "I will not be printed"
I will be printed
Env: GNU bash, version 4.2.46