Make your bash scripts crash hard on the first sign of trouble
Shell Scripting / Bash Tricks
Make your bash scripts crash hard on the first sign of trouble
🧩 The Challenge
You ever run a script that accidentally deletes the wrong directory because the ‘cd’ command failed silently and kept executing the next line? I’ve lost entire dev environments that way and it’s a nightmare to clean up.
💡 The Fix
Just flip on the internal bash safety switches at the very top of your script. It forces the shell to stop dead in its tracks the moment anything goes sideways instead of barreling ahead like a drunk driver.
set -euo pipefail
⚙️ Why It Works
Setting these options makes your script exit if a command fails, if you reference an undefined variable, or if a pipe produces an error. It basically turns bash from a “hope for the best” shell into a strict interpreter that values your data.
🚀 Pro-Tip: Don’t forget to wrap risky commands in ‘|| true’ if you actually want them to fail without killing the whole process.
Linux Tips & Tricks | © ngelinux.com | 10/10/2026
