Bash – why deleting bash history is not enough

bashbashrclinux

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?

Best Answer

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.