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
  • 2
    This is a good read - BashFAQ/088 - How can I avoid losing any history lines?
    – Inian
    May 3, 2018 at 6:53
  • @Yaron: That question is related, not the same, since I'm not addressing the race condition. Also, it doesn't have a very satisying answer...
    – einpoklum
    May 3, 2018 at 7:46
  • @einpoklum - the answer describes: How turn history on with every command - won't it answer your needs?
    – Yaron
    May 3, 2018 at 7:53
  • Read about bash eternal history.
    – user232326
    May 3, 2018 at 10:54

1 Answer 1

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.