ABOUT BASH HISTORY
##################
 
About how it works:
* Every Command Saved to RAM
* Then on exit from shell stores to $HISTFILE  (ram is flushed to histfile)
 
The mechanics:
* Command “history ” reads from $HISTFILE  and then from RAM, so shows you everything.
* HISTFILE is a variable that stores the location of where the HISTORY should be saved to.
 
About histfile:
* Its set on boot in your .bash_rc or other initializing scripts
* Each user has its own histfile usually like this: /user/USERNAME/.bash_history  and root has /root/.bash_history 
* Rarely on some systems you will see a system bash_history file located on the ROOT of the system, so its /.bash_history  – not sure under what circumstances it records, because being logged in as root just records stuff to /root/.bash_history 
 
(1) To read whats in the total history (before this session, and in this session[whats stored in ram]):
history

 

 
(2) To see whats stored in before this session:
cat $HISTFILE

 

(3) To see where your session is saving to:
echo $HISTfILE

 

(4) So to clear history of current session so it doesnt save, need to:
unset HISTFILE
exit
 
Then log back in
 
Whats unsetting mean? If you have a variable stored like X=5, if you unset it, the system forgets what X equals to, and eqauls to nothing, same here
unset HISTFILE ” prevents current session from being saved to the HISTFILE (dont worry the variable will be set backup on reboot)
After unsetting HISTFILE, it eqauls to nothing so when you exit out of the session and it trys to save to HISTFILE, which is blank, thus nothing is saved
 
(5) Other ways to not save current session (which is in ram) to the HISTFILE:
kill -9 $$
echo $$  will show you the PID of the session, so kill -9 $$  just kills the current session. Its a force kill so the system doesnt save anything.
 
(6) *** BEST WAY TO CLEAR ALL HISTORY FOR CURRENT USER*** 
This clears the history for this session and previous sessions for this user:
rm -f $HISTFILE 
unset HISTFILE 
exit

 

 
WANT TO LEARN MORE ABOUT IT:
IF YOU HAVE THE VARIABLE “PROMPT_COMMAND” set to ‘history -a ‘ THEN YOU NEED TO DO ONE MORE STEP BEFORE rm -f $HISTFILE, WHICH IS unset PROMPT_COMMAND – AND THEN OF COURSE AFTER WE NEED TO SET “PROMPT_COMMAND” BACK UP: MORE DETAILS ARE HERE:  Articles on bash

Leave a Reply

Your email address will not be published. Required fields are marked *