How do I clear the terminal History?
reset
or tput reset
only does things to the terminal. The history is entirely managed by the shell, which remains unaffected.
history -c
clears your history in the current shell. That's enough (but overkill) if you've just typed your password and haven't exited that shell or saved its history explicitly.
When you exit bash, the history is saved to the history file, which by default is .bash_history
in your home directory. More precisely, the history created during the current session is appended to the file; entries that are already present are unaffected. To overwrite the history file with the current shell's history, run history -w
.
Instead of removing all your history entries, you can open .bash_history
in an editor and remove the lines you don't want to keep. You can also do that inside bash, less conveniently, by using history
to display all the entries, then history -d
to delete the entries you don't want, and finally history -w
to save.
Note that if you have multiple running bash instances that have read the password, each of them might save it again. Before definitively purging the password from the history file, make sure that it is purged from all running shell instances.
Note that even after you've edited the history file, it's possible that your password is still present somewhere on the disk from an earlier version of the file. It can't be retrieved through the filesystem anymore, but it might still be possible (but probably not easy) to find it by accessing the disk directly. If you use this password elsewhere and your disk gets stolen (or someone gets access to the disk), this could be a problem.
I have tried history -c but the history comes back once we exit and reopens.This helped me.
cat /dev/null > ~/.bash_history && history -c && exit
This clears the history saved in the history file as well as the history in the current session (so that it's not saved to file when bash
exits). It then exits the shell. The next shell session will have no history.
Instead of removing all your history entries, type these commands in your terminal:
history -c
(for delete history)history -w
(save history)