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?
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.
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.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.
echo $HISTFILE
shows the one to edit.
Commented
Dec 1, 2018 at 14:27
open -e ~/.zsh_history
.
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.
nano ~/.bash_history
when you done,
ctrl+x
and come back to accept a right answer dude
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:
Regardless of which method you use, I would add one step: verify success!
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.
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