bashexitxfce

Run script before Bash exits


I'd like to run a script every time I close a Bash session.

I use XFCE and Terminal 0.4.5 (Xfce Terminal Emulator), I would like to run a script every time I close a tab in Terminal including the last one (when I close Terminal).

Something like .bashrc but running at the end of every session.

.bash_logout doesn't work


Solution

  • You use trap (see man bash):

    trap /u1/myuser/on_exit_script.sh EXIT
    

    The command can be added to your .profile/.login

    This works whether you exit the shell normally (e.g. via exit command) or simply kill the terminal window/tab, since the shell gets the EXIT signal either way - I just tested by exiting my putty window.