Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I routinely run multiple screen sessions on my Linux desktops and servers.

A problem with this is that when I grep through my command history, I find I issued a command in a different session, and have to detach and re-attach to get that history item.

Is it possible to 'force' the differently-updated histories from multiple sessions to all go to a central history?

share|improve this question
    
I ran into a similar issue whereby I was not able to view history between terminal sessions. Turns out my ~/.bash_history file was owned by root and not writable. Changing owner/group to my user fixed the issue: sudo chown <user>:<group> ~/.bash_history –  mateo Nov 25 '11 at 13:14
    
similar: stackoverflow.com/questions/103944/… –  lesmana Dec 3 '11 at 9:20

3 Answers 3

up vote 35 down vote accepted

There are two things you need to do:

  1. Insert the command shopt -s histappend in your .bashrc. This will append to the history file instead of overwriting it.
  2. Also in your .bashrc, insert PROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n" and the history file will be re-written and re-read each time bash shows the prompt.

EDIT: Thanks to e-t172 for the history -n trick

share|improve this answer
5  
Thanks for this. I improved it further using PROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n". This way, commands issued in other sessions immediately appear in the history of the current session (well, you need to press Enter first to update the history). –  e-t172 Sep 8 '09 at 10:41
1  
This is wonderful! Thanks for the tip! –  Anthony Giorgio Sep 10 '09 at 12:27
5  
I didn't have PROMPT_COMMAND defined previously, so I had to use PROMPT_COMMAND="history -a; history -n" to avoid errors. –  William Jackson Jun 2 '11 at 14:53

PROMPT_COMMAND is not defined by default in some distros. For example, it is defined in Arch, but not in Debian.

I have a bashrc in my Dropbox that I use while distro hopping, and it includes:

export PROMPT_COMMAND="$( [ '$PROMPT_COMMAND' ] && echo $PROMPT_COMMAND; )history -a; history -n"
share|improve this answer

Please don't use history -a; history -n, it does not work as you expect and will leave you with many duplicate, out of order commands in your history. A solution that works generally as expected is the following:

# unified bash history
shopt -s histappend
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Using a newline instead of a semicolon is also a short way of dealing with the missing/duplicate semicolon problem with PROMPT_COMMAND.

share|improve this answer

Your Answer

 
discard

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.