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 ls: cannot access /opt/non_existing_dir1: No such file or directory ls: cannot access /opt/non_existing_dir2: No such file or directory
Env: GNU bash, version 4.2.46