Reputation: 8424
How can I trigger a function when a command in bash returns an exit code of 1? I know set -e
at the top will just make my code terminate, but I want to call this function first. If the code runs ok I want it to exit normally without the function being called. I don't want to do a $?
check after every line. I'm sure there's an easy way to do this but I'm new to bash scripting so I don't know it offhand.
Upvotes: 6
Views: 287
Reputation: 530833
Set a trap on the ERR
pseudosignal:
set -e
error_handler () {
# do stuff here
}
trap error_handler ERR
Upvotes: 4