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?

  • 9
    history -d <line_number> didn't work for me, so just deleted the desired line and the one preceding it (that should start with #) from ~/.bash_history.
    – toraritte
    Commented Jun 30, 2021 at 20:10
  • Relating unix.stackexchange.com/questions/236094/…
    – Jeff Schaller
    Commented Aug 1, 2021 at 16:02
  • 9
    From macOS Catalina on, the standard shell is zsh and not bash and the history command works differently, so history -d <line> will not work. @toraritte answer becomes then: type vim .zsh_history and delete typing d once on the line you need to delete. To save and quit then type :x and hit Enter.
    – giotto
    Commented Aug 10, 2021 at 12:30

13 Answers 13


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

Additionally, if the line you want to delete has already been written to your $HISTFILE (which typically happens when you end a session by default), you will need to write back to $HISTFILE, or the line will reappear when you open a new session:

history -w
  • 1
    It worked. I think we need to source ~/.bashrc after modifying it...
    – B Seven
    Commented Sep 26, 2012 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
    Commented Sep 26, 2012 at 18:03
  • 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
    Commented Oct 22, 2012 at 3:54
  • 4
    It didn't work for me.
    – Felipe
    Commented Jun 20, 2020 at 5:17
  • 1
    @Gauthier because history is also a bash builtin. type history reveals that. Use man bash-builtins to find the man page for it. Commented Sep 28, 2021 at 14:57
  1. To clear all your history, use

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

    history -d linenumber
  • 12
    It didn't work for me.
    – Felipe
    Commented Jun 20, 2020 at 5:17
  • 1
    This could definitely use an explanation of what "line number" is and how to find it.
    – Clonkex
    Commented Sep 18, 2020 at 1:59
  • 2
    Note that you must start a new shell session in order for these changes to take effect. Commented Aug 10, 2021 at 22:20
  • 1
    @Clonkex the "line numbers" for each past command will be visible when running history Commented Aug 10, 2021 at 22:21
  • @DavidSawyer I see, this answer would make sense to someone familiar with history. I'm not familiar with it, as you can maybe tell.
    – Clonkex
    Commented Aug 11, 2021 at 0:17

Zsh on Mac

If you are using Zsh, history -d linenumber doesn't work to delete a specific line number in the command line history. However you can edit the history file. Close and reopen your terminal and edit the history like so:

nano ~/.zsh_history

See this for more details.

  • Same for tcsh. you can edit ~/.history
    – G Eitan
    Commented Jan 29 at 10:56
  • try ~/.bash_history too
    – Behrouz.M
    Commented May 28 at 2:46

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 '^\s*\d+') - 1);              
  • That seems a little complicated. Wouldn't history -d $( history | tail -n 1 | cut -f 1 -d " " ) be simpler?
    – seumasmac
    Commented Oct 3, 2015 at 1:40
  • 4
    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. Commented Oct 3, 2015 at 3:03
  • 2
    Any chance someone could help out with portin this to zshell?
    – Alex S
    Commented Oct 14, 2015 at 13:27
  • The question title says “a single line”, but the question body says “I enter something”.  Note that this approach will fail if the “something” that is entered is more than a single line. Commented Apr 10 at 19:22
  • @G-ManSays'ReinstateMonica' more than a single line or single command? If the former, what's a multi-line command that this wouldn't work for? Commented Apr 11 at 0:45

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
    Commented Jul 17, 2018 at 2:56
  • This may needlessly remove more than necessary, or may possibly not remove anything, depending on how the shell is configured.
    – Kusalananda
    Commented Apr 2, 2020 at 8:03

To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered the clear-text password as in the scenario above, find the line number in the history file and run this command.

$ history -d 2038

To delete or clear all the entries from bash history, use the history command below with the -c option.

$ history -c

Alternatively, you can use the command below to delete the history of all last executed commands permanently in the file.

$ cat /dev/null > ~/.bash_history

Also With Bash 5, you can delete a range aswell

history -d 511-520

  • 3
    history -d line number didnt work for me. I'm using mac. Commented Jan 27, 2020 at 10:13
  • You need to brew cask install jumpcut or else follow this Commented Jan 27, 2020 at 10:51

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 $$
  • This does not take the history that is written to $HISTFILE into account.
    – Kusalananda
    Commented Apr 2, 2020 at 8:02
  • @Kusalananda unless you have taken special steps to write to the $HISTFILE immediately, this should work. The ONLY think I have taken into account is the $HISTFILE. What experience have you had (with my proposal) that causes you concern? Commented Apr 13, 2020 at 15:39
  • 1
    If the thing that the user entered was in a previous session, killing the current shell would not remove that single thing from the history file. Also, I'm on systems where the admins have changed the default setup so that commands are written to the history file after every given command. We know nothing about the current user's setup.
    – Kusalananda
    Commented Apr 13, 2020 at 15:41
  • @Kusalananda your statement is true. However, I would hope that no one would ever try to kill the current process trying to affect the result of a past completed process. That's just illogical to expect. But it does suggest a slightly different phrasing to your original comment that would have avoided the confusion. "This does not affect the history that was written to $HISTFILE in previous sessions." In other words, "this concept is only good for preventing the situation, not correcting it." Thanks for clarifying, friend. Commented Apr 13, 2020 at 15:48
  • 1
    Well, it wouldn't affect the history written to $HISTFILE in the current session either, so I really don't see how that comment could be misinterpreted. The only thing you have to add to your answer is the assumptions under which the given solution solves the problem. There is really only one assumption, namely that the command that should be removed from the history hasn't yet been written to $HISTFILE. Then you may want to elaborate under what conditions the command would have been written to $HISTFILE.
    – Kusalananda
    Commented Apr 13, 2020 at 15:53

1- in bash terminal type

history # This will list all commands in history .bash_history file with line numbers


  987  cd
  988  ssh [email protected]
  990  exit
  991  cd

2- pick the CMD line number you want to delete

history -d 988

Note: if you want to delete for example last 3 CMDs, just pick the third line number from bottom ex: 988 and repeat the CMD history -d 988 3 times in sequence.

  • Welcome on U&L! This really looks similar to (a part of) the accepted answer (except for the suggestion about deleting consecutive commands). Is there anything you can add to make it less of a repetition?
    – fra-san
    Commented Mar 16, 2020 at 10:41
  • Sorry, no and for the first look I didn't understand the accepted answer [and you won't believe that I checked other answers with more details explained] so when I figured out that history command lists the used commands I just wanted to explain that!
    – KhogaEslam
    Commented Mar 16, 2020 at 12:26
  • 1
    No problem. Answers that happen to be duplicates of other answers may eventually be deleted and I just try to make sure people have room to deal with that possibility before it happens ;-)
    – fra-san
    Commented Mar 16, 2020 at 17:47
  • Alternate opinion. The question asked to delete a single entry, so I upvoted the ones that answered the question first, not as an afterthought.
    – gbarry
    Commented Dec 20, 2021 at 17:12

In Ubuntu (but I'm pretty sure, it will be work for other Linux distributions and also MacOS the same way) the bash history file can be simply edited in an arbitrary text editor:

$ nano ~/.bash_history

If you don't know, where it's stored, you can find it as follows:

$ echo $HISTFILE

Or you can just do it a bit more generic way:

$ nano $HISTFILE
  • This does not take into account that the current in-memory history of the shell would be written to the file when the shell exits. This would not delete a command from the history that is resident in the in-memory history of the shell.
    – Kusalananda
    Commented Apr 2, 2020 at 8:05

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.


If you have hstr (a way better reverse-i-search that can be installed with sudo apt install hstr), then it's really simple:

  1. Open your terminal and press Ctrl+r to search your history.
  2. Type some characters to search for the command you want to delete from your history, then use arrow keys to go down and highlight the item.
  3. Press the delete key, and press y to confirm.

If you want to delete a range of history lines, you can use the script below.

This 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

Quick steps:

  1. Find out where is your terminal's history file with echo $HISTFILE
  2. Open the file with your text editor
  3. Delete the sensitive lines you find there
  4. Save the file
  5. Close and reopen your terminal
  6. DONE: your history is clean! You can validate that by running the history command in your terminal

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .