Explain why .bash_logout won't run commands?

11

8

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:

#!/bin/bash
# ~/.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
fi

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

Droogans

Posted 2012-04-09T22:06:20.407

Reputation: 263

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 – 2012-04-09T22:34:09.250

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 – 2012-04-10T00:24:04.487

Answers

25

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

Chris Page

Posted 2012-04-09T22:06:20.407

Reputation: 2 793

1I 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 – 2012-04-10T13:55:48.640

6

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

user1686

Posted 2012-04-09T22:06:20.407

Reputation: 283 655