2

Using KDE Konsole as a bash terminal I would like to clear the history when I close the terminal (tab/application), note that I do need the bash history when Konsole is still open (to search it); I would like to have it cleared once the terminal is closed.

I often use the terminal for a long time and clear the history before I close the terminal, I am looking for a way to automate the clearing of the history.

I found a similar questions on how to clear the history or how to disable it like How do I close a terminal without saving the history?, however I found nothing helpful for my situation. The difference here is that i do need the history file while the terminal is running, setting unset HISTFILE disable the history file after that command is ran and does not clear the history file itself. Note that the history file is needed while the session is running but when it get closed it need to be cleared.

How can we clear the bash history when the terminal gets closed?

11
  • Why is disabling it per unix.stackexchange.com/q/25049/5132 not helpful? How does it not do exactly what you want? – JdeBP May 27 '20 at 21:49
  • Maybe it is being saved as it goes along. You need to disable it. Probably in ~/.bashrc. Do you have a history add in (for example) your shell prompt? – ctrl-alt-delor May 27 '20 at 22:08
  • @JdeBP i still need the history during the session, disabling it completely is not intended. – origami May 27 '20 at 22:15
  • 1
    erase .bash_history and put a trap in bashrc trap "history -c" EXIT – Yunus May 27 '20 at 22:26
  • 5
    Does this answer your question? How do I close a terminal without saving the history? – André Werlang May 28 '20 at 17:34
6
  1. erase .bash_history

    cat /dev/null > .bash_history
    

    or

    >.bash_history
    
  2. add a trap to .bashrc

    trap "history  -c" EXIT
    
4
  • 2
    Useless use of cat. – rexkogitans May 28 '20 at 9:24
  • @rexkogitans just as it's useless to improve a cmd that it will be run once in life , the thing is about clearing history onexit , and the op has right to erase his bash_history or just keep some useful history entries that will stay persistance ! – Yunus May 28 '20 at 10:42
  • 2
    When someone comments on a useless use of cat, implying that somehow such a comment is denying people rights is a rather absurd response. – JdeBP May 28 '20 at 14:31
  • @JdeBP sorry if you got it like that , but i didn't mean any offence or denaying people! – Yunus May 28 '20 at 15:27
8

You can use a .bash_logout file to specify commands that should be executed when logging out (see e.g. here for more information).

6

The bash documentation (see man bash) includes this about history,

Shell Variables The following variables are set by the shell:

[...]

HISTFILE The name of the file in which command history is saved [...]. The default value is ~/.bash_history. If unset, the command history is not saved when a shell exits.

So, to allow command history during a session but to prevent it being saved, simply unset this variable. You can do this in any or all of .bash_profile, .profile or .bashrc:

unset HISTFILE
1

export HISTFILE=/dev/null

It leaves old history untouched. It comes handy when you accidentally type a password or other data you do not want to share in .bash_history :-)

Not the answer you're looking for? Browse other questions tagged or ask your own question.