4. Store Bash History Immediately
By default, Bash only records a session to the .bash_history file when the session terminates.
This means that if you crash or your session terminates improperly, you lose the history up to that point.
Use $PROMPT_COMMAND variable to save each command right after it has been executed.
Append the following line to ~/.bashrc file, if the variable $PROMPT_COMMAND hasn’t been set yet:
PROMPT_COMMAND='history -a'
Append the following line, if the variable $PROMPT_COMMAND has already been set:
PROMPT_COMMAND='$PROMPT_COMMAND; history -a'