As far as I can tell, the history of an individual shell is only appended to the .bash_history file when the shell is closed. This means that I frequently lose individual shells' histories on crashes / kernel panics.

Why does the history only get written on closing a session? Is there a way to have each command written at the time of execution? Is there any reason I should not want to have each command written at the time of execution (e.g., performance issues when writing to an enormous bash_history file)?

(I'm using Mac OS X 10.6 and running many tabbed terminals simultaneously with iTerm. I have histappend set along with some other options.

export HISTSIZE=1000000
export HISTTIMEFORMAT="%d/%m/%y %T "
shopt -s histappend


1 Answer 1


You're missing one line in your .bashrc. Add:



shopt -s histappend

PROMPT_COMMAND is executed just before the next prompt is printed (IIRC). You're right about when the history is written, by the way. The modified prompt command will flush the history.

  • I'd rather recommend to put PROMPT_COMMAND="$PROMPT_COMMAND;history -a" in .bash_profile since it is an recursive expression and for each level of nesting of bash it will add next unnecessary history -a call.
    – czerny
    Jul 12, 2017 at 17:55
  • 2
    this results in a syntax error for ";history -a"
    – elig
    Jun 7, 2019 at 3:54

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.