Reputation: 23114
I want to execute some commands every time I exit from bash, but cannot find a way to do it. There is a ~/.bash_logout file for when you are logging out, but normally we use interactive shell instead of login shell, so this is not very useful for this purpose.
Is there a way to do this? Thanks!
Upvotes: 2
Views: 102
Reputation: 531325
You can trap the EXIT signal.
exit_handler () {
# code to run on exit
}
trap 'exit_handler' EXIT
Techinically, trap exit_handler EXIT
would work as well. I quoted it to emphasize that the first argument to trap
is a string that is essentially passed to eval
, and not necessarily a single function name. You could just as easily write
trap 'do_this; do_that; if [[ $PICKY == yes ]]; then one_more_thing; fi' EXIT
rather than gather your code into a single function.
Upvotes: 5