Stop letting your bash scripts break because a command failed silently

Shell Scripting / Bash Tricks

Stop letting your bash scripts break because a command failed silently

🧩 The Challenge

Most of us write scripts where one failed command leads to a cascade of errors that destroy your production data. I once watched a script happily try to delete files in a directory that didn’t exist because the ‘cd’ command failed and the script just kept on truckin’.

💡 The Fix

You need to tell bash to exit immediately the moment anything returns a non-zero exit code. It forces your script to be honest about failure instead of pretending everything is fine.

set -euo pipefail

⚙️ Why It Works

Setting -e stops execution on errors, -u treats unset variables as errors, and -o pipefail makes sure that if any part of a pipeline fails, the whole thing counts as a failure. Without the last one, you’re just asking for trouble when a grep or awk dies halfway through a piped operation.

🚀 Pro-Tip: Stick this at the very top of every script you write; it’s a zero-cost insurance policy against catastrophic typos.

Linux Tips & Tricks | © ngelinux.com | 8/23/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted