Stop your shell scripts from dying silently when a command fails

Shell Scripting / Bash Tricks

Stop your shell scripts from dying silently when a command fails

🧩 The Challenge

You write a thirty-line script, run it, and it silently executes only the first command before giving up because the second one failed. I have spent way too many nights debugging production deploys because a hidden ‘cd’ failure didn’t stop the rest of the script from nuking the wrong directory.

💡 The Fix

Use set -e to tell your shell to exit immediately if any command returns a non-zero status. It is the single best way to keep your scripts from blindly charging ahead into a disaster.

#!/bin/bash
set -euo pipefail
# Rest of your script goes here

⚙️ Why It Works

Adding that specific combination of flags handles errors, forces you to define variables before using them, and makes sure piped commands don’t mask failures in the middle of a chain. It basically forces the shell to actually respect the exit codes you usually ignore.

🚀 Pro-Tip: Use set -x while you’re debugging if you want to see exactly which line is causing the crash in real-time.

Linux Tips & Tricks | © ngelinux.com | 11/7/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted