For security reason you may want to clear the history file and the screen when you hit CTRL+D or type logout command as you don’t want to left content on screen (i..e avoid an information leak). Some Linux distro may clear the screen but others do not clear the screen when you logout.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists. Basically you can use this file as the individual login shell cleanup file, executed when a login shell exits.

There is a simple solution to this problem, open your ~/.bash_logout file:
$ vi ~/.bash_logout
Append any one of the following command:
You can also reset your history by appending following commands:

First one will clear bash history and 2nd command will clear mysql command history using shell REDIRECTION operator >.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 17 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
17 comments… add one
  • dbr May 15, 2007 @ 22:12

    The >~/.bash_history thing seems a slightly odd way of doing things, why not put “unset HISTFILE” in your .bashrc file, since .bash_logout will be run about the time .bash_history is written, so it may (though probably not) end up wiping the file before it’s written.
    Or what I prefer to do, rm .bash_history and then :
    ln -s /dev/null .bash_history

  • J May 16, 2007 @ 8:40

    maybe history -c is an option…

  • 🐧 nixCraft May 16, 2007 @ 9:40

    >ln -s /dev/null .bash_history
    sounds good idea

    >maybe history -c is an option…
    never thought of that, good idea 🙂

    Appreciate your posts!

  • naveenvasireddy May 21, 2007 @ 4:56

    really this is very useful for me

  • NVRAM May 24, 2007 @ 15:19

    > maybe history -c is an option…
    I like it.

    I’ve typically used:

    > why not put “unset HISTFILE” in your .bashrc file, since .bash_logout
    > will be run about the time .bash_history is written [snip]

    Similar idea, but IIRC I’ve had problems with unsetting the HISTFILE — but that was probably ~15 years ago under Korn shell.

    I also tried a touch-then-delete approach in my profile, I think that one worked fine, but the $RANDOM is my default.

    Incidentally, I try to always do this for root, since on rare occasions passwords are on the command line.

  • jeff Sep 27, 2007 @ 18:27

    ever heard of kill -9 $$ ?

    It logs you out and clear the history.

    It seems to kill bash, echo $$ gives you the bash PID

    A friend of mine showed me this once, but I never completely get how it works, if someone can explain, please do so…

  • SHAFE Apr 2, 2008 @ 14:01

    This is the Wright and the Best OPtion to clear the command History

    history -c

  • Shankar May 23, 2008 @ 4:50

    Thanks shaff, It works

  • Harka May 23, 2008 @ 12:15


    history -c will clear the entire bash history.
    To only clear the current session history use:
    history -r.

  • erica Aug 11, 2008 @ 10:09

    you are a genius shafe

  • Kumar Chetan Sharma Nov 20, 2008 @ 7:49

    even after running clear or history -c, which I find is the best option to add to ./bash_logout, I can still scroll my putty screen and see what all I have done. Is there any way to completely erase this information.

  • Vinnie Jul 15, 2009 @ 15:38

    Kumar asked about clearing the information from his putty window.

    To do so –
    1. Right click on the top bar of the putty window
    2. A Context menu will appear
    3. Select “Clear Scrollback”

    This will erase all details of your session from putty’s scrollback buffer.

  • gnudiego Sep 7, 2010 @ 0:19

    It seems like .bash_logout doesn’t work in psudo-terminals like gnome-terminal or konsole. Anybody has idea why it doesn’t?

  • Leila Nov 29, 2010 @ 14:09

    gnudiego you are correct, it does not work with gnome-terminal which is installed as a default for linux mint. I am considering removing the gnome-terminal but would like to know what the best alternative would be.


  • Phissure Aug 18, 2011 @ 17:15

    I placed “history -r” in .bash_logout

    I then added the following to .bashrc, just in case something did get written to .bash_history. It checks if you have any other sessions open, if you don’t it clears the .bash_history file.

    if [ w | grep -c $(whoami) = 1 ]; then

  • Ken Mos Sep 19, 2011 @ 13:08

    Please update me with putty

  • My Aug 19, 2012 @ 0:17

    Harka – You are The man!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum