Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I often open lots and lots of Terminals [Right now I have 7 open on this workspace] and I often search history with grep to find a command I've just written recently, but I don't want to hunt down the terminal and then scroll up and hunt for it more, etc.. Sometimes my terminals close without 'exit,' and everything I've written is lost [Sometimes I've needed something I'd written in a terminal that was killed].

So is there a way to make it so each terminal writes to .bash_history immediately? or at least once a minute, or something like that?

share|improve this question

3 Answers 3

up vote 21 down vote accepted

Found something here: Cuberick: Update Bash History in Realtime

It says to put those commands in the .bashrc config:

shopt -s histappend

The first command changes the .history file mode to append. And the second configures the history -a command to be run at each shell prompt. The -a immediately writes the current/new lines to the history file.

Related for zsh:

share|improve this answer
well this kind of works. I tried some more testing and it seems to only work on the first command. does it work correctly for you? – Matt Oct 16 '11 at 15:03

Try putting this into your .bashrc:

shopt -s histappend                      # append to history, don't overwrite it
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

Credit here:

history -c clears the history of the running session. This will reduce the history counter by the amount of $HISTSIZE. history -r read the contents of $HISTFILE and insert them in to the current running session history. this will raise the history counter by the amount of lines in $HISTFILE.

I think it means that the commands are available almost immediately (you have one terminal, write echo 1 second terminal echo 2, first echo 3 and upon pressing down arrow twice, you should have echo 2 available. You must issue a command in a given terminal to have acces to what has been written.

share|improve this answer
Brilliant! Thanks. Wonder why those options aren't documented in man history... – artfulrobot May 17 '13 at 12:31
@artfulrobot: This command is a bash built-in so it is described in man bash. You can also use help history. – pabouk Jul 2 '14 at 9:32
I have my zsh configured to work like this: Each shell tracks its OWN history (and before that, the combined history), and every command is appended to combined history. This is different and better than always reloading history for every command for every shell, because I tend to have a different "thread of work" or "job" for each shell terminal. I think that is the difference between this with -r and -c vs just having -a. – Steven Lu Feb 25 at 9:40
So what exactly are you using? history -c; history -r? I guess it is better for you, it would be better for me only sometimes, I often use several shells to do one job so I prefer to have the history as much combined as possible. I actually mind that command is written to history only after it finishes. typically, when I start a ssh session to one host and want to open another one while the first is still running, I have to retype it because it has not been written to history yet. – sup Feb 25 at 10:36
I think I never stuck around long enough to figure out how to make bash simultaneously track parallel threads of history (per shell instance basis) while immediately writing history to the main history file. Zsh makes it much easier to do so, plus in addition I have defined a zsh function to write out a lot of fancy metadata to an enhanced-history file of my devising. It's become rather indispensible. Anyway, the mechanism there is precmd() and preexec() for zsh get sent args describing the entered command. It then becomes trivial to direct that to files. history -a might suffice – Steven Lu Jun 3 at 4:03

I have a large history file with about 100000 entries, and the variants that clear the history list and read the whole history file (using history -c and history -r) introduce a noticeable (maybe 0.2 second) delay before the prompt is displayed. Using history -n so that only new lines are read from the history file is faster:

shopt -s histappend
PROMPT_COMMAND='history -a;history -n'

PROMPT_COMMAND does not have to be exported because it is a shell variable.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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