Explain why .bash_logout won't run commands?
Solution 1:
Assuming you’ve verified that it’s a login shell (shopt login_shell
):
~/.bash_logout is only run if it you explicitly exit the shell with exit
or logout
, or by typing Control-D to enter an end-of-file at the command prompt. If you close the terminal emulator, processes are sent SIGHUP, and bash doesn’t run ~/.bash_logout in that case.
If you want to perform work any time bash exits (and whether it’s a login shell or not), use trap foo EXIT
. The most convenient way to do this is to put your code in a shell function, e.g.,:
print_goodbye () { echo Goodbye; }
trap print_goodbye EXIT
Solution 2:
~/.bash_logout
is only run by a login shell (such as the one you get on tty sessions, or with bash -l
). It is ignored by non-login shells, which most terminals run.
See the manual page of bash(1), under "INVOCATION", for further detail.
If you do not want to keep persistent history, just unset HISTFILE
, or set HISTFILE=/dev/null
. This will automatically discard history on exit, while still allowing it to be kept for the current shell (HISTSIZE=100
).