Writing Reliable Bash Scripts
Bash gets a bad reputation, usually earned by scripts written without guardrails. A few habits make shell scripts genuinely reliable.
Start with strict mode
Put this at the top of every script:
#!/usr/bin/env bash
set -euo pipefail
It makes the script exit on errors, treat unset variables as failures, and fail a pipeline if any stage fails. You will catch bugs immediately instead of halfway through a deploy.
Quote everything
Unquoted variables are the source of most “it worked on my machine” surprises.
# Fragile
cp $src $dest
# Safe
cp "$src" "$dest"
Keep functions small
Treat shell like any other language: name things, return early, and keep each function focused on one job.
- One function, one responsibility.
- Validate inputs at the top.
- Prefer
localvariables.
Reliable Bash isn’t about clever one-liners — it’s about removing the sharp edges so the script does exactly what it says.

