Site icon New Generation Enterprise Linux

Stop hunting for the exit status in your messy shell scripts

Shell Scripting / Bash Tricks

Stop hunting for the exit status in your messy shell scripts

🧩 The Challenge

You finally finish a massive automation script, run it, and it fails silently without telling you which part bombed out. It’s infuriating to go back and add error checking everywhere after the fact.

💡 The Fix

Use a simple trap function that catches non-zero exit codes automatically. It saves you from manual debugging for every single command.

trap 'echo "Error at line $LINENO: command failed with exit code $?"' ERR
set -e

⚙️ Why It Works

Setting the -e flag tells the shell to bail out immediately on any error, and the trap grabs the line number before the script actually terminates. You get an instant pointer to the exact line that broke your flow.

🚀 Pro-Tip: Put that trap at the very top of your script right after the shebang so you don’t miss the setup phase.

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

0 0 votes
Article Rating
Exit mobile version