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:
OR (||) and AND (&&) Operators
Use && and || to chain commands based on exit statuses:

command1 && command2
Runscommand2only ifcommand1succeeds (exit code 0).command1 || command2
Runscommand2only ifcommand1fails (non-zero exit code).
The set -e Flag
Without set -e, a failing command won’t stop the script:
set -e preserves the failing command’s exit code:

| Exit Code | Description |
|---|---|
| 0 | Success |
| 1 | General error |
| 2 | Misuse of shell builtins |
| 127 | Command not found |
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:

-e, -u, -o pipefail), your Bash scripts will:
- Fail fast on errors
- Prevent usage of uninitialized variables
- Detect broken pipelines
Links and References
- Bash Reference Manual – Exit Status
- ShellCheck – Static Analysis for Shell Scripts
- Advanced Bash-Scripting Guide