Stop getting burned by failed commands in your bash scripts

Shell Scripting / Bash Tricks

Stop getting burned by failed commands in your bash scripts

🧩 The Challenge

You write a twenty-line script to move files around, the first command fails, and the script just happily keeps running like nothing happened. I’ve nuked entire directory structures before because I forgot that bash doesn’t care if your commands exit with an error.

💡 The Fix

Just add a single command at the top of your script to make the shell exit the second anything returns a non-zero exit code. It forces your script to stop before it makes a mess of your production server.

set -e

⚙️ Why It Works

Setting this flag tells the shell to treat any command returning a non-zero status as a fatal error, which stops execution immediately. It saves you from those “oh no” moments where a failed mkdir leads to writing files into the wrong location.

🚀 Pro-Tip: Use set -u as well to catch those times you accidentally reference an unset variable.

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted