1

I understand that command line history in the case of bash is saved in .bash_history.

When you open a terminal emulator window in Ubuntu/MacOS and work in it, the history is not saved in .bash_history yet but when you close the window, the history of the session will be appended to the end of the .bash_history file.

My question is: where are the history saved during the session, before I close the emulator window?

The reason I care is that sometimes I mess up wile working in it and the terminal window simply freeze. When I force close it, I lose all the history contained in the session. I want to be able to retrieve them.

2
  • @mosvy, not on MacOS. It's more complicated there. See unix.stackexchange.com/q/473581/135943
    – Wildcard
    Dec 4, 2019 at 23:10
  • So, what's the value of PROMPT_COMMAND in bash on MacOS? I guess they may be already calling history -a from it, in which case the OP can retrieve the history of recently crashed sessions from ls -t ~/.bash_sessions/ | head?
    – mosvy
    Dec 4, 2019 at 23:36

1 Answer 1

0

You could try one of the suggestions here, addition to your .bashrc is immediate, although there are some disadvantages.

I added the following to ~.bashrc and have experienced no real issues

HISTSIZE=5000
HISTFILESIZE=10000
shopt -s histappend

export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

Whether this works in MacOS or not I don't know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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