Home Blog Bash Safe Scripts (Strict Mode)

Safe Scripts (Strict Mode)

Overview

These options can be set inside a bash script to make it safer or increase strictness. They can also be set on-the-fly by calling bash with the corresponding flags, i.e. bash -eu <script_path>.

set -eu
set -euo pipefail
set -Eeuo pipefail

Explanation

set -e

The -e option makes the script exit as soon as a command returns a non-zero exit code.

set -u

The -u option makes the script exit as soon as a variable is used before being set.

set -o pipefail

The -o pipefail option makes the script exit as soon as a command in a pipeline fails.

set -E

When using -e without, the script will exit as soon as a command fails, but it will not trigger the ERR trap when that happens. Setting -E prevents this behavior from happening, and will trigger the ERR trap as well.

Bonus

The -x option can be used to print the commands executed by the script. This is useful for debugging.

Sources