15

I used to think deleting my bash history was enough to clear my bash history, but yesterday my cat was messing around the right side of my keyboard and when I got back into my computer I saw something I typed a month ago, then I started to press all the keys like crazy looking for what could've triggered it. Turns out UPARROW key shows my bash history even after deleting .bash_history.

How can I delete my bash history for real?

  • 8
    .bash_history is overwritten every time you close a bash shell. So if you start bash, delete .bash_history, and log off, you'll have accomplished exactly nothing. – Shadur Jan 13 '18 at 14:05
  • 14
    Your cat deserves a huge reward. You've been using bash for how long without discovering it has command-line recall and editing? Do you just retype every command line from scratch every time or something? That would make the shell very unpleasant to use. – cas Jan 14 '18 at 3:57
  • @cas ten years. Now that you say it I press Uparrow to repeat commands, but I understood it as kind of a 'short term' memory no more than a few lines long. I wasn't warned that deleting .bash_history won't delete bash history. – bunden Jan 14 '18 at 15:31
  • Deleting ~/.bash_history really does delete ~/.bash_history....but (unless you take steps to prevent it, like clearing the in-memory history by running history -c just before you logout or setting HISTFILE=/dev/null or just unseting it) bash will just save its current history again when it exits. BTW, the HISTSIZE env var tells bash how many lines of history to keep in memory, and HISTFILESIZE tells bash the maximum number of history lines to save. both of these can be set in, e.g., your ~/.bashrc. PS: your cat deserves a 2nd reward for teaching you a valuable lesson :) – cas Jan 14 '18 at 16:14
  • @cas I think deleting .bash_history should delete bash history. It seems intuitive to me. What it doesn't seem intituive is history -c, history -w, unsetting variables.. that's nuts from the user experience perspective. – bunden Jan 14 '18 at 16:51
26

In some cases (some bash versions), doing a:

$ history -c; history -w

Or simply

$ history -cw

Will clear history in memory (up and down arrow will have no commands to list) and then write that to the $HISTFILE file (if the $HISTFILE gets truncated by the running bash instance).

Sometimes bash choose to not truncate the $HISTFILE file even with histappend option unset and $HISFILEZIZE set to 0.

In such cases, the nuke option always works:

history -c; >$HISTFILE

That clear the history list of commands recorded in memory and all commands previously recorded to file. That will ensure that the running shell has no recorded history either in memory or disk, however, other running instances of bash (where history is active) may have a full copy of commands read from $HISTFILE when bash was started (or when a history -r is executed).

If it is also required that nothing else (no new commands) of the present session would be written to the history file, then, unset HISTFILE will prevent any such logging.

|improve this answer|||||
  • 1
    Editing HISTFILE and doing history -c && history -r would replace your current history with manually modified history. In some cases, that may be better than throwing all the history away. – Mikko Rantalainen Apr 9 '18 at 10:52
9

bash has a session history in memory which is written to file if the shell variable HISTFILE is set to a filename when bash exits.

If you delete the file pointed to by HISTFILE, unset that variable, and exit bash, then that shell session will not leave any persistent history.

Failing to unset the HISTFILE variable but deleting the file would just empty the persistent history, but the current session's history would be saved when the shell exits.

|improve this answer|||||
  • 3
    A history -c; history -w will clear both memory and file history. Unsetting HISTFILE is only required if no further commands should be logged to the history (not exactly what was asked IMhO). – Isaac Jan 13 '18 at 15:20
-1

"How can I delete my bash history for real?"

Exit bash, start a different shell, delete the bash history file.

or

logout, log into a different account that has write permission on the history file, delete the history file.

|improve this answer|||||
-2

While leaving the shell, do it all in once, like

echo "" > ~/.bash_history && history -c && exit
|improve this answer|||||
  • @roaima, you're apsolutely right, thank you. Corrected. – user853069 Feb 18 '19 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.