Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 52 down vote accepted

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


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


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
share|improve this answer
It worked. I think we need to source ~/.bashrc after modifying it... – B Seven Sep 26 '12 at 18:00
@BSeven Yes, all bash settings are stored in RAM while the shell is running. The rc files are only for storing persistent settings for the next time the shell starts. – jw013 Sep 26 '12 at 18:03
There are edge cases when this will not work, but under default configurations it should. – jordanm Sep 26 '12 at 19:27
@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: – jordanm Oct 22 '12 at 3:54
@Moberg $num is a shell parameter (a.k.a. variable). I can see how my example would be confusing if you didn't know about shell parameter expansion, so I edited the code to use a concrete example instead. – jw013 Aug 26 at 13:05
  1. To clear all your history, use

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

    history -d linenumber
share|improve this answer

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);              
share|improve this answer
That seems a little complicated. Wouldn't history -d $( history | tail -n 1 | cut -f 1 -d " " ) be simpler? – seumasmac Oct 3 at 1:40
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 at 3:03
Any chance someone could help out with portin this to zshell? – Alex S Oct 14 at 13:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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