How do I edit bash shell terminal history?
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.
- 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. - Make the file visible by following these instructions, for instance, open it as any other file, modify it, and save.
- 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.
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:
- 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!
- Exit Terminal.app
- Re-open Terminal.app
- 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.