Site icon New Generation Enterprise Linux

Catch Command Errors Before They Byte You

Environment & Shell Customization (Aliases/Functions/.Bashrc)

Catch Command Errors Before They Byte You

đź§© The Challenge

How many times have you run a long command, maybe piped a few things, then moved on, only to find out way later that the first step failed silently? And you just skimmed the output and missed that critical non-zero exit code. It’s a huge time sink.

đź’ˇ The Fix

Get your shell prompt to scream at you when the last command *didn’t* exit successfully. It’s a simple change to your `PS1` that slaps the exit status right in front of you when it’s anything but zero. You’ll stop chasing down phantom failures.

export PS1='[\u@\h \W$(if [[ $? != 0 ]]; then echo " \$?"; fi)]\$ '

⚙️ Why It Works

See, your shell sets `$?` to the exit status of the *last* foreground command. This little snippet captures that `$?` *just before* the prompt redraws, and if it’s not zero (meaning success), it sticks it right there in your face. It’s brilliant.

🚀 Pro-Tip: Make that exit code red. Or blink. Whatever gets your attention. Add some ANSI escape codes to the echo.

Linux Tips & Tricks | © ngelinux.com | 10/22/2026

0 0 votes
Article Rating
Exit mobile version