Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I've been wondering how to run these two lines of code everytime I close an open instance of Terminal:

history -c
cat /dev/null > ~/.bash_history

I export HISTFILE=5 on startup, but still want to flush that out when I'm done.

I've tried looking around a bit in a couple of places, and haven't had much luck.

I run Linux Mint, and would also note here that I ran into a similar issue with .bash_profile; eventually, I discovered I needed to place all start up code in .bashrc, so maybe that has something to do with it. Here's my .bash_logout file:

# ~/.bash_logout: executed by bash(1) when login shell exits.

#this does nothing on exit...
echo 'logout'; sleep 2s

# when leaving the console clear the screen to increase privacy

if [ "$SHLVL" = 1 ]; then
    history -c
    cat /dev/null > ~/.bash_history
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q

I've tried re-arranging this script many ways, I'm not sure if I don't understand how bash works, and if any of this is running in the first place. Does the fact that I run Xserver make bash consider Terminal something that isn't a log-out on exit?

It does say that I am not logged in, which would make sense:

$> shopt login_shell
login_shell     off
share|improve this question
As a general comment, if you’re testing whether or not a shell config script is getting run, I recommend you put the echo command at the start, before it does anything else that could interfere with the echo getting run. An error could cause the script to exit before it gets to the end. – Chris Page Apr 9 '12 at 22:34
I was assuming from the way you phrased this, and the fact that you’re checking for SHLVL=1, that you already verified that it’s a login shell. Is it? What does shopt login_shell say? – Chris Page Apr 10 '12 at 0:24
up vote 14 down vote accepted

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
share|improve this answer
I feel like a wasted a perfectly good problem on my bad question. I'll ask again if I get past your first sentence and still have trouble. Sheesh. – Droogans Apr 10 '12 at 13:55

~/.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).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .