Keeping bash history in sync on disk and between multiple terminals

I rely on my ~/.bash_history extensively to avoid retyping things, both short-term and long term (when I want to look up something I typed yesterday or last week).

First, an obvious necessity is

shopt -s histappend

in ~/.bashrc, so that bash just keeps on adding to the end of the ~/.bash_history file rather than obliterate the file at regular intervals.

By default, bash reads from your ~/.bash_history once when you log in to a terminal, and it updates ~/.bash_history once when you log out. I dislike both of these default behaviors.

Writing out to ~/.bash_history only when you log out is no good, because what if your terminal dies unexpectedly? You'll lose the history of any commands you typed there. This can happen if you kill a bash process, or if the machine powers off unexpectedly, or who knows what. So, to manually update your ~/.bash_history immediatety, at any time you can use this:

history -a

which tells bash to append to ~/.bash_history any commands in the current terminal that aren't already in there.

The second behavior of bash I dislike is that it reads from the history file only at login, mostly because I always have many terminals open at once. I run a command from one terminal, then run a different command from another, switch back and forth etc. These terminals may be appending all kinds of nice new lines to the end of my ~/.bash_history, but because bash never reads the history file except at login, they're never accessible except in the terminal where the command was originally run. Luckily you can force bash to re-read your history file via:

history -n

which tells bash to read any new lines that have appeared in ~/.bash_history since the last time it looked.

Now, say I type a really long command, e.g. a huge long Perl one-liner, in one terminal, and I want to then access and run that command in another terminal via bash history. With bash's default behavior, I can't do it; each terminal has its own history. What I could do is type history -a in the first terminal, and history -n in the second terminal; then it'd work wonderfully.

I find the thought of having to do that all the time tedious, though. Better would be for bash to do it for me every time I run a command. You can accomplish this by putting something like this in ~/.bashrc:

export PROMPT_COMMAND="history -n; history -a"

PROMPT_COMMAND lets you specify a command that bash will run every time it shows you a fresh command prompt, i.e. every time you run a command and the command finishes. So the above tells bash to read any new lines that have appeared in ~/.bash_history since the last time it read it, and then append the last-run command from this terminal to ~/.bash_history, every time you run a command.

So now, if you type a command in one terminal, and want to access it via the history of another terminal, run a command in the other terminal (or just hit Enter) to trigger PROMPT_COMMAND, and then your history will be nicely up-to-date and synchronized with any other terminals you have open. Almost certainly, you'll never notice the tiny bit of overhead caused by bash constantly reading and writing to ~/.bash_history.

See man bash for more info on the history builtin.

12 Comments

http://gravatar.com/avatar/8de7452456bb13b797994edfbfdd0dbb.jpg?d=identicon
bed says:

Thank you!

shopt -s histappend was exactly what I searched for.

I had found this moths ago at a German blog but could not find it again... Today the "last logout wins" bites me again :(

Now, this should be history :)

Nov 12, 2007 04:56 AM PST
http://gravatar.com/avatar/4d84ec3981443dfd9c287e845b60d2ce.jpg?d=identicon
Brian says:

Pun intended?

Nov 12, 2007 08:02 PM PST
http://gravatar.com/avatar/be816c375a6161d8a078005d78b30e1a.jpg?d=identicon
Anonymous says:

Your use of PROMPT_COMMAND works nicely, but it has one downside: Ctrl-O becomes much less useful. Normally, if you enter a series of commands, you can go back to the first one, and hit Ctrl-O repeatedly to run the current command and retrieve the next. If each terminal saves each command right after running it, then you can't necessarily reconstruct the series of commands run on one terminal.

Nov 22, 2007 07:32 PM PST
http://gravatar.com/avatar/4d84ec3981443dfd9c287e845b60d2ce.jpg?d=identicon
Brian says:

Yeah, another bad thing is when for example you open a terminal just to start some server. Then you open another terminal and play around for a while. Then you want to restart the server process, so you go back to that terminal and Ctrl+C it and hit UP to go back in your history to re-run the server command. But oops, now a lot of commands are in between you and the one you want.

Maybe it'd be nice to script a way to easily choose which behavior you want on a terminal-by-terminal basis.

Nov 22, 2007 07:39 PM PST
http://gravatar.com/avatar/698ff67822bdb3859fbe850b73cec6f4.jpg?d=identicon
Much Ado About Nothing » Blog Archive » Sick of losing bash history? says:

[...] this post by Brian today, I will be doing this on all my [...]

May 14, 2008 00:23 AM PDT
http://gravatar.com/avatar/06ed5e372807c202700762e537bf7734.jpg?d=identicon
Mike Golvach says:

Nice post,

That's one of those things I've just dealt with, but never bothered to look into.

shopt -s histappend works perfectly and now I don't have to wonder which session I'm getting my history from :)

Thanks!

, Mike

May 18, 2008 07:57 PM PDT
http://gravatar.com/avatar/12bb5230cc0ab1c6ed5cfb6929e2deed.jpg?d=identicon
Kalmi says:

I find it a lot less confusing this way: alias u='history -n' export PROMPT_COMMAND="history -a;"

Jun 29, 2008 10:55 PM PDT
http://gravatar.com/avatar/08de20da6f40881f0f53cd0e666f681b.jpg?d=identicon
nickwe says:

Great, it bother me for so long, never took the time to search for a work around and I just get to this page looking for something else and now I've resolve an old problem!

Many Thanks!

Feb 27, 2009 07:15 AM PST
http://gravatar.com/avatar/5bf64a54f40647bd16c0a27eef201d83.jpg?d=identicon
Hand Histories says:

I am still new to Linux and I had the same problem as I use many terminals simultaneously. The combination of shopt -s histappend and export PROMPT_COMMAND="history -n; history -a" is very handy. Thank you, I had been looking for something like that for a while. I mean in linux shell, you always have to look at your history, it saves a lot of time.

Mar 26, 2009 04:55 PM PDT
http://gravatar.com/avatar/f8f04c0eac2e84a0c354df00b37f8bf5.jpg?d=identicon
cde says:

I had the same problem. Well, half the problem. I just added history -a instead of both -n and -a. With both, you have the problem Brian explained, and after years of being accustomed to one history file per active terminal, it would be too annoying to change that around. By just adding the -a to the bashrc, you can do -n only when you need to.

Apr 09, 2009 09:21 PM PDT
http://gravatar.com/avatar/e8341d0089896ad4e9aa2576c98025cc.jpg?d=identicon
Balan K says:

Thanks for the information. I am just looking for a way to manage my multiterminal environment. shopt -s histappend should be handy tool.

Jun 06, 2009 05:54 PM PDT
http://gravatar.com/avatar/0156e6f7374d1635ac8a3ea61962ac99.jpg?d=identicon
ontario motels says:

Great, it bother me for so long, never took the time to search for a work around and I just get to this page looking for something else and now I've resolve an old problem! always keep th good work going on your side.. congrats

Jun 07, 2009 10:49 AM PDT

Speak Your Mind

Preview

Commenting Help

Email / Avatar

  • Supply your email address and your Gravatar will be used.
  • I will never email you and your address won't be published.

No HTML allowed!

All HTML is auto-escaped. Use Markdown. Examples:

  • *emphasis* = emphasis
  • **strong** = strong
  • [link](http://foo.bar) = <a href="http://foo.bar">link</a>
  • `code in backticks` = code in backticks
  •     code indented 4 spaces =
    code indented four spaces
  • > Angle-brace quoted text =
    Angle-brace quoted text