Instantly share code, notes, and snippets.

Embed
What would you like to do?
Idea for improving error messaging in `bash` scripts

We often use set -e at the top of our bash scripts to stop early when a command fails. This works great but it doesn't provide messaging as to what command failed.

We realized that we could leverage trap with the sigspec ERR to perform the same job (or maybe extend the behavior):

trap -- "history 1" ERR
not found
# No command 'not' found, did you mean:
#  Command 'nop' from package 'graphviz' (main)
#  Command 'hot' from package 'hopenpgp-tools' (universe)
#  Command 'dot' from package 'graphviz' (main)
#  Command 'jot' from package 'athena-jot' (universe)
#  Command 'net' from package 'samba-common-bin' (main)
#  Command 'note' from package 'note' (universe)
# not: command not found
#   502  2017-01-06T19:25:27-0800 not found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment