delete a line from your shell history

history -d
If you're a moron like me, sometimes your fingers get away from you and you, for example, enter your password when you're already authenticated to ssh-agent, sudo, etc., and your password ends up in shell history. Here's how to get it out.
Sample Output
ssh host
-bash: mypass: command not found
    1  mypass
    2  history
history -d 1
    1  history

By: sud0er
2009-04-27 20:19:09

These Might Interest You

  • By default bash history of a shell is appended (appended on Ubuntu by default: Look for 'shopt -s histappend' in ~/.bashrc) to history file only after that shell exits. Although after having written to the history file, other running shells do *not* inherit that history - only newly launched shells do. This pair of commands alleviate that. Show Sample Output

    $ history -a #in one shell , and $ history -r #in another running shell
    b_t · 2011-11-05 01:19:30 0
  • This could be added to .bashrc. Background: Linux usually saves history only on clean exit of shell. If shell ends unclean, history is lost. Also numerous terminals might confuse their history. With this variable set, history is immedeately written, accessible to all other open shells.

    PROMPT_COMMAND="history -a"
    danam · 2009-10-21 12:33:25 0
  • I rarely need this, but I have a hard time remembering the command when I need it. Admit it. This has happened to you. Yes this is bad, and you better clean up now. Borrowed from Show Sample Output

    alias histdel='history -d $((HISTCMD-2)) && history -d $((HISTCMD-1))'
    StefanLasiewski · 2010-07-02 00:20:44 3
  • #

    Using the "#" in shell is surprisingly useful. Some of the uses I found: a) As a visible copy buffer in shell history (caveat: do not use for passwords :-) b) To build complex commands until ready then hit the HOME, DEL, ENTER keys to run it c) Placing reference data into shell history (search for tags with CTRL-R TAGNAME) d) Putting aside a "work in progress" command to focus on another task (HOME # ENTER) Show Sample Output

    # indicates a comment in shell
    mpb · 2009-03-16 23:15:33 0

What Others Think

I have to use this one quite often. My usage isn't usually to remove passwords, but because I've accidentally pasted a large number of lines into a terminal window. Have to use a for loop to delete a bunch of lines.
fritz_monroe · 477 weeks and 4 days ago
Wow, commandlinefu just taught me the fix for an annoyance I have had for years! Thanks so much.
philiph · 477 weeks ago
You can also remove the command by moving to the command and delete that line. And followed by up/down key (not enter), the command will be erased.
ckclark · 430 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: