I found that my ~/.bash_history has record of all commands I typed in different terminal tabs. But when I do reverse-search (CTRL+r) it only shows commands from that particular tab from which I'm doing reverse search. How to enable reverse search so that it looks through all commands in ~/.bash_history rather than just current tab history?
1 Answer
Most of this is covered here.
To answer your precise question- By default, the history file is read once at shell startup, and written once at shell exit. To fetch commands that appeared in the history file since the shell start, use the history update command history -n
, or the combination history -a ; history -c; history -r
. The difference between those is how the histories are merged.
-
Thanks, do you mean to say I cannot do reverse -search in terminal tab A for the commands I typed in terminal tab B; unless I run
history -n
in terminal A? Commented Aug 10, 2016 at 3:33 -
1The "once" rule applies to both. To get commands from A to file: have A either exit, or explicitly call
history -a
. To get them from file to B: have B either start after file is updated, or callhistory -n
orhistory -r
(the merging order differs between those). This is explained in the other answer. Commented Aug 10, 2016 at 4:02 -
I have accepted this answer but I wish there was an easier way to do this without manually running
history
commands when working across tabs. Commented Aug 11, 2016 at 2:47 -
Thank you, but there is- see the linked question. The solution involves using
PROMPT_COMMAND
. Commented Aug 11, 2016 at 3:13
ksh
will do this; in window1 I can type a command and it immediately shows up in the history on window2. Sometimes you may need to press RETURN to make it resync properly. Indeed, if you have an NFS mounted$HOME
then this history update may appear across multiple servers.