Bash Strict Mode: Catch Silent Failures in Deployment Scripts
set -euo pipefail makes Bash scripts exit on errors, unset variables, and failing pipeline steps, but expected non-zero exits like grep no-match need explicit handling.
28 Sept 2026, 12:20 UTC

The Deployment Script That Reported Success but Deployed Nothing
A CI job ran a shell script that copied build artifacts, then uploaded them to a registry. The cp command failed because the target directory did not exist, but the script kept running, the upload step found nothing to send, and the job still printed "done." Weeks later, a production rollout used stale assets.
Shell scripts do not stop on errors by default. A command can fail, return a non-zero exit status, and Bash will simply move to the next line unless you tell it otherwise.
What `set -euo pipefail` Changes
Adding this line near the top of a script turns three common silent failures into immediate stops:
set -e: Exit the script immediately when a command returns a non-zero exit status.set -u: Treat references to unset variables as errors instead of expanding them to empty strings.set -o pipefail: Make a pipeline return the exit status of the first command that fails, not just the last command in the chain.
Together these are often called Bash strict mode. They do not catch logic mistakes, but they prevent a script from continuing after an obvious failure.
Worked Example: Pre-Flight Check Before Deployment
This script checks that a required environment variable is set, confirms a source directory exists, and verifies that a manifest file contains an expected version string. It is meant to run locally or in a CI runner before a deployment step.
#!/bin/bash
set -euo pipefail
# Fail if DEPLOY_ENV is unset or empty
: "${DEPLOY_ENV:?DEPLOY_ENV must be set to staging or production}"
SOURCE_DIR="/app/build/${DEPLOY_ENV}"
MANIFEST="${SOURCE_DIR}/manifest.json"
# Confirm the directory exists before continuing
if [[ ! -d "${SOURCE_DIR}" ]]; then
echo "Missing source directory: ${SOURCE_DIR}" >&2
exit 1
fi
# Confirm the manifest exists and mentions the expected version
if grep -q '"version": "1.0.0"' "${MANIFEST}"; then
echo "Manifest version confirmed for ${DEPLOY_ENV}"
else
echo "Expected version string not found in ${MANIFEST}" >&2
exit 1
fi
echo "Pre-flight checks passed"
Where to run it, permissions, and expected checks
- Run in a terminal or CI job with Bash available. Save as
preflight.sh. - Make it executable:
chmod +x preflight.sh, or run withbash preflight.sh. - Export the variable before running:
export DEPLOY_ENV=staging. - Check the result with
echo $?immediately after the script finishes. Zero means success; non-zero means the script stopped at a failing check. - Risk: with
set -e, any command that returns non-zero will abort the script. Commands where failure is expected, such asgrepfinding no match, must be wrapped in anifor followed by|| true.
Where Strict Mode Still Stays Silent
Strict mode has boundaries you should know before adding it to an existing script:
set -edoes not apply inside the condition ofif,while, oruntil, nor to every command in an&&or||list. A failing command inside aniftest will not abort the script by itself.set -ufails on positional parameters such as$1when no argument is supplied. Use${1:-}or check$#first.set -o pipefailcan abort a pipeline when an intermediate command returns non-zero, even when that result is expected. For example,grepreturns 1 when no match is found.
Verification Before Merging
Run these checks in a local shell before pushing the script to CI:
- Run
bash -euo pipefail preflight.shwithDEPLOY_ENVunset. Confirm the script exits with an unbound variable error. - Point
SOURCE_DIRat a missing path. Confirm the script stops instead of continuing to the manifest check. - Run
false | truein a strict-mode shell and checkecho $?. Confirm the pipeline returns non-zero because ofpipefail. - Run
grep -q 'absent' /etc/hostname || trueto confirm that an expected no-match does not abort the script when explicitly handled.
Takeaway
Start new automation scripts with set -euo pipefail so failures surface immediately instead of producing misleading success messages. For existing scripts, add the flags incrementally and run the verification checks above; expected non-zero exits such as grep with no match need explicit handling. The result is a script that stops loudly when something is wrong, rather than quietly deploying nothing.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.