Skip navigation

As in all things, it’s important to remember what happened before so a) we know how we got here and b) we know what to do going forward.  Now, this is a good life lesson, but for the purposes of this post, I’m actually talking about the history of commands you type at your bash prompt. Bash, by default, has an annoying habit of not writing out the commands you type into your shell immediately.  There is a good argument for its default behavior.  By batching everything up and only writing things out when  you log out, you get a unified view of what happened in that specific shell session.  However, if your work flow involves multiple windows (or screen sessions) and connections to remote servers that may last for days or weeks, likelihood is you’ll have commands you typed days or weeks ago that still haven’t been written to your .bash_history file.  If your shell dies ungracefully (because your connection to the host is broken, the shell process dies or is killed, or the host is rebooted for some reason) your history will go bye bye.  Since this is a good description of my work flow, I want my history updated immediately across all of my shell sessions.  And here’s how I do it:

shopt -s histappend PROMPT_COMMAND="history -a;$PROMPT_COMMAND"

Thanks to Shlomo at for his post on this.  Putting this in your .bashrc or .profile will set the shell option to append your history to .bash_history (or whatever you’ve configured HISTFILE to be) and configure bash to immediately append your history to your history file each time it’s ready to show a prompt.  All in all, a very handy configuration change that’ll make it much easier for me to avoid remembering things in much the same way Dr. Henry Jones did. . . by writing it down so I won’t have to remember.