181

I'm working in Mac OSX, so I guess I'm using bash...?

Sometimes I enter something that I don't want to be remembered in the history. How do I remove it?

299

Preventative measures

If you want to run a command without saving it in history, prepend it with an extra space

prompt$ echo saved
prompt$  echo not saved \
> #     ^ extra space

For this to work you need either ignorespace or ignoreboth in HISTCONTROL. For example, run

HISTCONTROL=ignorespace

To make this setting persistent, put it in your .bashrc.

Post-mortem clean-up

If you've already run the command, and want to remove it from history, first use

history

to display the list of commands in your history. Find the number next to the one you want to delete (e.g. 1234) and run

history -d 1234
  • It worked. I think we need to source ~/.bashrc after modifying it... – B Seven Sep 26 '12 at 18:00
  • 3
    @jw013 I set PROMPT_COMMAND to history -a, in that case it is already written to the history file, rather than on exit under normal configuration. Specifically: mywiki.wooledge.org/BashFAQ/088 – jordanm Oct 22 '12 at 3:54
  • 3
    After deleting a single line, you need to write out the current history to your $HISTFILE using history -w. Otherwise when you exit, the deleted line is still there! – Felipe Alvarez Jan 18 '17 at 2:39
  • 1
    +1 to the comment above; using history -w after history -d {num} was exactly what I needed -- that critical tidbit should be added to the answer, otherwise a user might think a temp delete from the current shell is actually permanent. – michael Jul 11 '17 at 3:58
  • 1
    @BobStein history -w is necessary for entries that already have been written to the .bash_history file. How to reproduce (e.g. on CentOS 7 with stock configuration): 1. login 2. enter command echo foo 3. logout 4. login again 5. delete entry for echo foo 6. logout 7. login again: entry is still there. Depending on how you configure your bash history perhaps you can reproduce this issue in other ways - i.e. if you provoke history writing before logout. – maxschlepzig Oct 12 '18 at 19:53
45
  1. To clear all your history, use

    history -c
    
  2. To delete a single line, use

    history -d linenumber
    
6

You always can edit and remove entries from ~/.bash_history, useful when you want to remove either one entry or more than one entry

  • 2
    Most likely these entries will be reintroduced when you exit the shell. – l0b0 Jul 17 '18 at 2:56
5

I have this in my ~/.bashrc, which makes the command $ forget delete the previous command from history

function forget() {                                                              
   history -d $(expr $(history | tail -n 1 | grep -oP '^ \d+') - 1);              
}
  • That seems a little complicated. Wouldn't history -d $( history | tail -n 1 | cut -f 1 -d " " ) be simpler? – seumasmac Oct 3 '15 at 1:40
  • 1
    history | tail -n1 is the history command itself, so deleting that number gets the wrong entry. However, history -d $( history | awk 'END{print $1-1}' ) combines the line select, field select, and subtraction. – dave_thompson_085 Oct 3 '15 at 3:03
  • Any chance someone could help out with portin this to zshell? – Alex S Oct 14 '15 at 13:27
5

If you want to forget the entire bash session, you can kill the current bash process. Since the variable $$ hold the pid of the current shell, you can do:

kill -9 $$
1

You need to write the changes after you cleared the history. And if you wouldn't like to have the history wipe command in your history then you need to run the command like that:

history -c && history -w && logout

Good luck.

-1
Adding another point suppose if you want to delete a range of history lines

You can use below script. 

Below example will delete history output from line 1 to line 150.

for i in `history | awk 'NR > 1 && NR <=150{print   $1}'`; do history -d $i; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.