11

How do I edit my bash history in Mac OS X? I entered a password in that window by mistake and do not want to remove my whole history.

13

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 ~/.bash_history -a /Applications/TextEdit.app. 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.

  • I am getting "permission denied". When I do a sudo, I am not able to see all the commands entered. – Karthick S Apr 5 '13 at 15:46
  • 1
    @KarthickS You're getting that doing what exactly? – Thecafremo Apr 5 '13 at 15:55
  • If I look at the contents of .bash_history the contents are different from the output of a history command. I recognize both, the the contents of the file are older. What happens if multiple command windows are open? They don't seem to share the same command history. P.S. I did verify I'm using bash. – Victor Engel Dec 1 '18 at 14:23
  • I think I found my answer. ECHO $HISTFILE shows the one to edit. – Victor Engel Dec 1 '18 at 14:27
9

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.

  • 2
    This does not work on Yosemite with ZSH. – Paulo Oliveira Dec 1 '15 at 10:06
  • The question was about bash, not zsh. Read carefully! – Dmitri Mar 1 '16 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. – Paulo Oliveira Mar 2 '16 at 14:39
2
nano ~/.bash_history

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

0

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.

You must log in to answer this question.

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