how to delete last command from bash history

If you have ever typed something into a command prompt that you wished you hadn’t – you may find it useful to know that you can delete it from ~/.bash_history very easily.

The command:

history -d offset

will delete the history entry at position offset.

# history
1 cd
2 history
3 ls -alhF
4 history
5 wget username:password@private.ftp.com/secret/file.tar.gz
6 history

so to delete the wget command (which contains a password) – just use:
history -d 5

# history -d 5
# history
1 cd
2 history
3 ls -alhF
4 history
5 history
6 history -d 5
7 history

But suppose you KNOW you’re about to enter a command you don’t want to go into history. It’d be nice if you could just tack a little “hideme” modifer onto the front or tail of your command and be done with it. Unfortunately from what I’ve been able to google there is no such feature built into history or bash.

Naturally I made one.

TMP=$(history | tail -1 | awk ‘{print $1}’) && history -d $TMP && \
paste_in_shell_and_replace_this_with_whatever_you_want_to_hide

Rather than holding down backspace, you may find it useful to know that in bash Ctrl-W will delete from the cursor to the beginning of the previous word. Or if you think you’re going to use it A LOT you may try to put a little function/alias into your .bashrc:

func_hide ()
{
TMP=$(history | tail -1 | awk ‘{print $1}’) && history -d $TMP
}

alias hideme=’func_hide’

Dig the sneaky:

# history
1 cd
2 history
3 ls -alhF
4 history
5 history
6 history -d 5
7 history
8 vi .bashrc
9 history
# hideme && mysecretcommand
# history
1 cd
2 history
3 ls -alhF
4 history
5 history
6 history -d 5
7 history
8 vi .bashrc
9 history
10 history

About these ads

Tags:

5 Responses to “how to delete last command from bash history”

  1. clayg Says:

    http://thoughtsbyclayg.blogspot.com/2008/02/how-to-delete-last-command-from-bash.html

    ???

  2. Papou Says:

    Thanks for your article.
    I discovered by chance that bash command lines starting with a blank character are not stored in history.

  3. Dave Says:

    I believe zsh will do the ‘hide a command’ bit: just preface the command with a space. I changed over to zsh and have been very happy with it.

  4. krisluminar Says:

    LOL @ “Dig the sneaky”

  5. Liviu Chircu Says:

    The solution you came up with for “hiding” the next command amused me quite a lot, especially more as I knew the easy way to do it. So, to hide our next command from bash history, just type a ” ” (blank space) before your command:

    “mysql -uroot -pMySecretPass” will be recorded in .bash_history
    ” mysql -uroot -pMySecretPass” will not :).

    Have fun and happy hacking,
    Liviu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: