Stop fighting over which shell variable is actually set
Shell Scripting / Bash Tricks
Stop fighting over which shell variable is actually set
🧩 The Challenge
You’re debugging a massive script and suddenly realize a critical variable didn’t make it to the function scope, leaving you staring at an empty string instead of a path. I’ve wasted hours tracking down silent failures because bash happily proceeds with a null value instead of blowing up.
💡 The Fix
Use parameter expansion to force bash to throw an error and kill the script if a variable is unset or empty. It saves you from those nasty “rm -rf /$VAR” moments where $VAR turned out to be nothing.
${VAR:?Error message here}
⚙️ Why It Works
Bash looks at the question mark operator as a trigger to exit the shell immediately with your custom message if the variable is missing or null. By putting this directly in your script execution flow, you stop execution before a bad command can do any real damage.
🚀 Pro-Tip: Use this for environment variables like DATABASE_URL or BACKUP_PATH at the very top of your script to fail fast.
Linux Tips & Tricks | © ngelinux.com | 11/9/2026
