40

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?

2
  • 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, 2011 at 13:14
  • similar: stackoverflow.com/questions/103944/…
    – Lesmana
    Dec 3, 2011 at 9:20

4 Answers 4

43

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

4
  • 6
    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). Sep 8, 2009 at 10:41
  • 9
    I didn't have PROMPT_COMMAND defined previously, so I had to use PROMPT_COMMAND="history -a; history -n" to avoid errors. Jun 2, 2011 at 14:53
  • 2
    please see the superuser.com/a/734410/250287 answer for the correct history -a, -c, and -r commands.
    – mtd
    Dec 16, 2014 at 3:16
  • 2
    Just an alert to the newcomer: after using history rewriting for a while, I realized that I was often issuing the wrong commands; when you see in the previous line some command, you expect up arrow to repeat it, and with this config it is no longer always the case. Mar 14, 2015 at 15:37
17

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
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.

7
  • history -a, -c, and then -r is correct...this should be the accepted answer
    – mtd
    Dec 16, 2014 at 3:15
  • 1
    The export is not necessary. Because interactive shell options are supposed to be set from ~/.bashrc which is called from every sub shell, so inheritance is not necessary.
    – dolmen
    Mar 30, 2015 at 14:00
  • 2
    Is there a good reason not to just use PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND" instead?
    – Six
    Jun 3, 2016 at 8:52
  • That ordering would require one to store the last exit value before running the history commands, since many prompt commands rely on or display it. Jun 3, 2016 at 13:49
  • 2
    This generally works but does have an interesting functionality, namely that you've got to ask for history twice before getting the fully updated history included commands from other windows. Dec 28, 2020 at 14:05
0

None of the solution here worked for me, but what did work was just using:

cat ~/.bash_history | grep something

instead of

history | grep something

Kudos to this SO answer.

-1

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:

PROMPT_COMMAND="$( [ '$PROMPT_COMMAND' ] && echo $PROMPT_COMMAND; )history -a; history -n"
2
  • An awful solution to a basic shell programming problem. The right way: PROMPT_COMMAND="$PROMPT_COMMAND; history -a; history -n"
    – dolmen
    Mar 30, 2015 at 13:56
  • Better: PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''history -a; history -n'
    – dolmen
    Mar 30, 2015 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .