Skip to main content
Bash is incredibly flexible for automation, but its default behavior can mask failures. Unlike languages with structured exception handling, Bash continues executing even when commands fail, often returning a misleading exit code of zero. Enabling Strict Mode ensures your scripts fail fast, catch unset variables, and correctly handle pipeline errors.

Enabling Strict Mode

Three shell options form the core of Strict Mode:
  • set -e
    Exit immediately if any command returns a non-zero status.
  • set -u
    Treat unset variables as an error and exit immediately.
  • set -o pipefail
    Return the exit status of the first failed command in a pipeline.
You can combine these flags in a single command:
Before diving deeper, let’s review how Bash’s logical operators influence control flow.

OR (||) and AND (&&) Operators

Use && and || to chain commands based on exit statuses:
The image explains the use of "OR" (||) and "AND" (&&) operators in scripting. It shows that command2 runs if the first command is unsuccessful with "OR", and if successful with "AND".
  • command1 && command2
    Runs command2 only if command1 succeeds (exit code 0).
  • command1 || command2
    Runs command2 only if command1 fails (non-zero exit code).

The set -e Flag

Without set -e, a failing command won’t stop the script:
Enabling set -e preserves the failing command’s exit code:
Linux reserves certain exit codes for common errors:
The image shows a list of reserved exit codes used in scripting, detailing their numbers and meanings, such as "Success" for code 0 and "General error" for code 1.
For a full list, see the Bash manual.

The set -u Flag

By default, using an unset variable expands to an empty string. With set -u, Bash treats it as an error:
Always initialize variables or test their existence to avoid unexpected script exits.

The set -o pipefail Flag

By default, pipelines only report the exit status of their last command, potentially hiding earlier failures:
The image illustrates a process flow in strict mode scripting, showing two commands connected by a pipe, with "Stderr" and an output display labeled "2".
To catch errors anywhere in the pipeline, combine all three flags and handle failures explicitly:
With Strict Mode enabled (-e, -u, -o pipefail), your Bash scripts will:
  • Fail fast on errors
  • Prevent usage of uninitialized variables
  • Detect broken pipelines
—resulting in more reliable and maintainable automation.

Watch Video