Debugging Bash Scripts: Gracefully Handling `set -e` Errors

2025-07-27

This article presents a neat trick for gracefully handling errors triggered by `set -e` in Bash scripts. By using `trap 'echo "Exit status $? at line $LINENO from: $BASH_COMMAND"' ERR`, you can print information like the error line number, failing command, and exit status when the script encounters an error, making debugging easier. This leverages Bash-specific features: `$LINENO`, `$BASH_COMMAND` environment variables, and the `ERR` trap condition. Other shells like sh may behave differently and might not fully support this functionality.

Development script debugging