Saturday, February 16, 2008

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




This is (hopefully) the final update to an OLD post.  Sorta "pre-StackOverflow" internet.  

Checkout this question instead:
http://superuser.com/questions/384366/remove-a-certain-line-from-bash-history-file

^posted in the comments

The below is preserved only for hysterical accuracy


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

And naturally someone smarter than me came along and found a better way to do it - THANKFULLY they posted a comment here to help us out (thanks Mitch!):

history -d $((HISTCMD-1)) && \
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.

What I don't get, is that according man bash HISTCMD should be the CURRENT history number:

HISTCMD
The history number, or index in the history list, of the current command.


and yet in ALL my tests $HISTCMD is the "index in the history list, of the current command" +1

But it can still lead to two two useful aliases:

alias hideme='history -d $((HISTCMD-1))'
alias hideprev='history -d $((HISTCMD-2)) && history -d $((HISTCMD-1))'

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
# echo password && hideme
password
# echo password
password
# hideprev
# 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


I know the blog's kinda been on Linux kick lately - some of that is coming from the new job - I'm using Linux more. But, I've been working on a little project in Eclipse - Java/SWT - and I'm getting to a point where I may have some useful learnings to post coming out of that. Or maybe not...

Internet Tablet, and all my xbox's are running fine...

I'd like to throw out some props to Ivie for sending me an email about one of my posts that she read. I try to post stuff that I myself have trouble finding out there on the interwebz - so it's always nice to hear from someone that finds it useful - thanks Ivie!