When and how is bash_history appended to

bashcommand history

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

)

Best Answer

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

PROMPT_COMMAND="$PROMPT_COMMAND;history -a"

after

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.

Related Question