how to clear bash history and what to watch out for

In a pinch, you can clear all your bash shell’s command history by typing this command:

history -c

However, there are a few things about how bash shell’s history that worth a closer look.

The bash shell stores the command history in a file, this is typically ~/.bash_history. It is a simple text file with one command per line. Whenever an interactive instance of the bash shell is created, the bash shell loads the history into the memory. As long as the shell is running all new typed commands are appended in the history, but they only exist in memory. The history will be written to ~/.bash_history when the shell is terminated (or if you issue the history -w command, whichever comes first).

There is nothing wrong with that, as long as you use only one instance of the bash shell at a time. Consider this: you log into your computer and open two terminal windows. You do some work on the first shell, then you do some other work on the second shell. When done, you want to clear all recorded history and exit. You go to the first shell and type history -c to clear the history, and then exit. Then you go to the second shell and simply type exit, thinking you already have cleared the history. You think you have cleaned up everything, but no, if you check the contents of your ~/.bash_history all the history from the second bash instance will be there, looking at you right in the eyes.

One thing you can do is to scrupulously make sure that you execute history -c before exiting every bash instance you are running. But this is kind of stressful, plus you may always run into the case of forgetting to clear the history, because people simply forget.

Another thing you may try is to add the history -c command in your ./bash_logout script, but you have to keep in mind that it works only with login shells, like the shell you are given from the console, or when running bash with the –login parameter. This will NOT work with the shells opened by terminal emulators (like the GNOME Terminal, for example).

A more effective approach would be to simply add a crontab entry that will empty the ~/.bash_history file in a reasonable frequency. This will do:

*/1 * * * * cat /dev/null > ~/.bash_history

This command in your crontab will cause the bash shell’s history file to be emptied every one minute. Please not that you cannot run history -c from crontab, because history is in fact an internal bash shell’s command, not a real program that can be executed from crontab.

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to how to clear bash history and what to watch out for

  1. strasser says:

    Nice post clear and concise.

    Thank you!

  2. Rob says:

    Thank you for this post. It’s just what I was looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>