17

I entered a password in a command line pro or in a terminal window by mistake and do not want to remove my whole history. My shell is bash.

How do I edit my bash history in Mac OS X?

5 Answers 5

18

Editing the .bash_history is just a matter of opening the file with an editor. There are a couple of ways you could achieve this.

  1. In the Terminal just type vi ~/.bash_history, then edit the file as you want, and save. This option could be tricky, if you're not used to vi, of course.
  2. Make the file visible by following these instructions, for instance, open it as any other file, modify it, and save.
  3. In the Terminal just type open -e ~/.bash_history. This command will open the file with TextEdit, you can choose any other text editor, of course. Modify the file and save.

For the changes to be noticeable just restart the bash.

2
  • 2
    I think I found my answer. echo $HISTFILE shows the one to edit. Commented Dec 1, 2018 at 14:27
  • 1
    @VictorEngel That was helpful; thanks. For me, it was open -e ~/.zsh_history.
    – Ryan
    Commented Oct 19, 2023 at 13:25
13

In terminal type history and note the line number. Next call history -d noted-line-number. You can check that the line is deleted using history again.

3
  • 3
    This does not work on Yosemite with ZSH. Commented Dec 1, 2015 at 10:06
  • The question was about bash, not zsh. Read carefully!
    – User366
    Commented Mar 1, 2016 at 18:01
  • 1
    @DmitryDupelov you are right, sorry to bother. Anyway, it's a reminder to future readers (that may miss that detail as well). Thank you for correcting my comment. Commented Mar 2, 2016 at 14:39
3
nano ~/.bash_history

when you done, ctrl+x and come back to accept a right answer dude

1

I think that Dmitry's answer is ideal if done before saving the history - by default, saving occurs on shell/terminal exit.

If you may have already saved the history to ~/.bash_history, then I prefer Thecafremo's answer, but I would suggest adding a step of exiting Terminal to ensure that none of the bash instances have loaded the history and will re-save it. This is especially tricky if you have multiple terminals and have enabled any sort of command-by-command history update.

Background:

  • bash saves the history within an instance of the shell
  • bash can write this history to a file, which typically happens when you exit the shell/Terminal
  • bash can read from a file into the history, which typically happens when you open a shell/Terminal
  • it's possible to change the default behavior so that history propagates sooner, which can migrate your BAD_TEXT to the file and to other windows

Regardless of which method you use, I would add one step: verify success!

  1. Exit Terminal.app
  2. Re-open Terminal.app
  3. Confirm the offending text is not in the history - preferably in a way that will not create additional history. That is, don't grep BAD_TEXT .bash_history! This new command would go into history. I'd just open .bash_history in your favorite editor and search within that for BAD_TEXT.

This should give you peace of mind that you have eliminated BAD_TEXT no matter what kind of crazy bash setup you may encounter.

1

There are several histories for example bash and zsh. You can do this with nano. To do this, simply open the terminal

With this you edit the bash history

nano $HOME/.bash_history 

With this you edit the standard zsh history

nano $HOME/.zsh_history 

If you want, you can do the whole thing using TextEdit.

Bash: open -t $HOME/.bash_history

Zsh: open -t $HOME/.zsh_history

1
  • 2
    Let’s get this in a question asking for zsh history for best effect.
    – bmike
    Commented Apr 6, 2021 at 10:35

You must log in to answer this question.

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