Site icon New Generation Enterprise Linux

Stop accidentally nuking files with poorly written rm commands

Shell Scripting / Bash Tricks

Stop accidentally nuking files with poorly written rm commands

🧩 The Challenge

Everyone has had that moment of pure panic after running a script with a variable that turned out to be empty, causing rm -rf /path/to/$VAR/* to turn into a full-system disaster. It feels like your soul leaves your body the second you hit enter.

💡 The Fix

You can make bash exit immediately if a variable you are trying to use is unset or null. It saves your bacon by stopping the script dead in its tracks before it does something stupid.

set -o nounset

⚙️ Why It Works

Adding this to the top of your scripts forces the shell to throw an error and bail out whenever it hits an undefined variable. It forces you to write cleaner code instead of just hoping your variables are populated.

🚀 Pro-Tip: Combine this with set -o errexit and you’ll catch most of your dumb mistakes before they cause real damage.

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

0 0 votes
Article Rating
Exit mobile version