This question already has an answer here:

IIANM, bash saves its history to ~/.bash_history (or to $HISTFILE) on exit from a login shell session. But - what if you want to occasionally persist it for fear of the shell getting prematurely killed (as in SIGKILL or power failure)? Is this possible without burdening the system or the shell session experience?

marked as duplicate by Kiwy, Jeff Schaller, Romeo Ninov, Gilles bash May 3 '18 at 12:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


By default, Bash saves it's history on exit from shell. Thus it will lose it's current history if it's killed non-gracefully.

You can save your current Bash history by running:

$ history -a

Knowing that, you can make Bash save it's history after each executed command, by running history -a after each executed command. One way to do is via the Bash prompt:

PROMPT_COMMAND='history -a'

This works because the Bash prompt will execute $PS1, $PROMPT_COMMAND, and more, on each new prompt.

Here is a great blog post that got me onto the idea: https://sanctum.geek.nz/arabesque/better-bash-history/

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