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?
-
2This is a good read - BashFAQ/088 - How can I avoid losing any history lines?– InianMay 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...– einpoklumMay 3, 2018 at 7:46
-
@einpoklum - the answer describes: How turn history on with every command - won't it answer your needs?– YaronMay 3, 2018 at 7:53
-
Read about bash eternal history.– user232326May 3, 2018 at 10:54
1 Answer
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/