0

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?

4
0

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.