About nixCraft

Topics

Shell tip: Clear the command history and screen when you log out

Posted by Vivek Gite [Last updated: May 15, 2007]

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:
/usr/bin/clear_console
OR
/usr/bin/clear
You can also reset your history by appending following commands:
>~/.bash_history
>~/.mysql_history
sync;

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

Tell us how we're doing: Please answer a few questions about your experience to help us improve nixCraft.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. dbr Says:

    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

  2. J Says:

    maybe history -c is an option…

  3. vivek Says:

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

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

    Appreciate your posts!

  4. naveenvasireddy Says:

    really this is very useful for me

  5. NVRAM Says:

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

    I’ve typically used:
    HISTFILE=/do/not/use/$RANDOM/$RANDOM

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

  6. jeff Says:

    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…

  7. SHAFE Says:

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

    history -c

  8. Shankar Says:

    Thanks shaff, It works

  9. Harka Says:

    Hi,

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

  10. erica Says:

    you are a genius shafe

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.