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?

origami
  • 69
  • Why is disabling it per https://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
  • @ctrl-alt-delor thanks for the edit :) ... i do search the history for the current session but once I am done using the terminal (often a long time...) i do cleat the history manually... i was looking for a way to automate that "clear" – origami May 27 '20 at 22:20
  • If you need to clarify the question then edit it. – ctrl-alt-delor May 27 '20 at 22:22
  • 1
    erase .bash_history and put a trap in bashrc trap "history -c" EXIT – Yunus May 27 '20 at 22:26
  • @origami upvoting is not an automatic process. You can likely attract upvotes by improving a little on your question, e.g. include the reason why the other question you found doesn't suit your needs - having it only in the comments is not helpful, because comments are difficult to read and may be deleted without notice - or referring to other tutorials and how-to's you have found (again, with explanation why they don't suit your needs). – AdminBee May 28 '20 at 09:53
  • The other question is not about disabling it completely. It's about stopping it being saved to file, exactly as here. So, again, how do the likes of https://unix.stackexchange.com/a/25050/5132 not do exactly what you want? – JdeBP May 28 '20 at 14:25
  • 5
  • The difference here is that i do need the history 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. The history file is needed while the session is running but when it get closed it need to be cleared. some answers here solve the question :) – origami May 30 '20 at 14:52
  • @origami this is what unsetting HISTFILE does (it tells bash not save history contents after exiting - it does not affect the history mechanism whilst using bash). What may be confusing you that .bash_history may already have entries in it. In which case rm ~/.bash_history and then the unset HISTFILE setting will no longer re-create it. – Drav Sloan Jun 03 '20 at 12:27

4 Answers4

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

AdminBee
  • 22,803
bat
  • 148
8
  1. erase .bash_history

    cat /dev/null > .bash_history
    

    or

    >.bash_history
    
  2. add a trap to .bashrc

    trap "history  -c" EXIT
    
AdminBee
  • 22,803
Yunus
  • 1,684
  • 2
    Useless use of cat. – rexkogitans May 28 '20 at 09: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
  • @Yunus Do you know what the trap history is accomplishing? Could someone add explanation to this otherwise fine answer? – Stephan Kristyn Jan 17 '22 at 16:52
  • 1
    @stevek-pro trap used to execute something on exit or other signal, according to this case when you exit terminal it will clear history before being saved to histfile – Yunus Jan 19 '22 at 00:51
  • Thanks for the explanation. The trap is not working for me for some reason. This, however works for me: echo 'unset HISTFILE' >> ~/.bashrc – Stephan Kristyn Mar 18 '22 at 16:14
7

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
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
2

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 :-)

DevilaN
  • 1,966