Site icon New Generation Enterprise Linux

Stop your scripts from running amok with set -e and set -u

Shell Scripting / Bash Tricks

Stop your scripts from running amok with set -e and set -u

🧩 The Challenge

You think your backup script is finished, only to find it ran half the commands, failed silently, and left your production database in a state of absolute chaos. I have spent way too many nights fixing things because a script decided to keep running after a command crapped out.

💡 The Fix

Just add a couple of flags at the top of your scripts to force them to stop the second something goes wrong or when you reference a variable that does not exist. It turns your reckless scripts into something that actually respects your server’s state.

#!/bin/bash
set -euo pipefail

⚙️ Why It Works

Setting -e makes the script exit on error, -u errors out on undefined variables, and pipefail ensures that if any command in a pipeline fails, the whole pipeline registers as a failure. You essentially stop the script from blindfolding itself.

🚀 Pro-Tip: Add the -x flag if you need to debug a script in real-time; it prints every command before running it so you can see exactly where it hits a wall.

Linux Tips & Tricks | © ngelinux.com | 9/4/2026

0 0 votes
Article Rating
Exit mobile version