bash: Clearing History

The excellent bash shell keeps a history of recently executed command lines and lets users cycle through them with up/down arrow, edit commands, and re-execute past commands.

Every once in a while, I make a mistake and type a password at a bash prompt — today it happened because I had a remote session waiting for a password, which timed out, so I typed my password at the “Password:” prompt, but my workstation noticed the connection was down and dropped me into bash, which failed to execute my password (because it’s of course not a valid command), and helpfully cached it on disk for future shells to take advantage of.

The quick fix is “history -c“, to flush the whole history (and Command-K in Terminal or whatever’s necessary in a different terminal program to clear the screen and scroll-back buffer).

A less drastic step is to use “history | tail” to find the line number of the bad command, and “history -d 503” (or whatever the appropriate line number is) to clear just the bad line, preserving the rest of the history. Further details are available with “man bash“.

1 Comment »

  1. Mistoffeles said,

    December 21, 2006 at 2:12 pm

    You can just type ‘history [n]‘ to get a list of the last n lines of the history, including the command numbers, for instance if you have a history with 12 commands and type ‘history 3′ you will get:

    10 third last command 11 seond last command 12 last command

RSS feed for comments on this post · TrackBack URL

Leave a Comment

You must be logged in to post a comment.