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
Tags: Bash
September 11, 2008 at 7:23 am |
http://thoughtsbyclayg.blogspot.com/2008/02/how-to-delete-last-command-from-bash.html
???
May 24, 2009 at 1:58 pm |
Thanks for your article.
I discovered by chance that bash command lines starting with a blank character are not stored in history.
June 17, 2009 at 10:30 pm |
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.