Instantly load bash history of one shell into another running shell

$ history -a #in one shell , and $ history -r #in another running shell
By default bash history of a shell is appended (appended on Ubuntu by default: Look for 'shopt -s histappend' in ~/.bashrc) to history file only after that shell exits. Although after having written to the history file, other running shells do *not* inherit that history - only newly launched shells do. This pair of commands alleviate that.
Sample Output
## In shell one ##

$ find -iname "*pdf*"   
<some output>

$ history -a   #You don't need this if you set PROMPT_COMMAND="history -a;$PROMPT_COMMAND" manually or in your ~/.profile or ~/.bashrc

## Switch to another running shell ##

$ #Up arrow does not show the above 'find' command, so do following:
$ history -r
$ find -iname "*pdf*"   #Up arrow now shows the 'find' command

By: b_t
2011-11-05 01:19:30

These Might Interest You

  • I was surprised to find that with RedHat bash, I could not find any comment lines (begining with #) in my bash shell history. Surprised because in Mageia Linux this works. It turns out that RedHat's bash will keep comment lines if in my .bashrc, I define: export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?' Why have comment lines in shell history? It's a handy and convenient way to make proto-commands (to be completed later) and for storing brief text data that is searchable in shell history. Show Sample Output

    export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'
    mpb · 2011-10-18 19:58:39 0
  • This could be added to .bashrc. Background: Linux usually saves history only on clean exit of shell. If shell ends unclean, history is lost. Also numerous terminals might confuse their history. With this variable set, history is immedeately written, accessible to all other open shells.

    PROMPT_COMMAND="history -a"
    danam · 2009-10-21 12:33:25 0
  • #

    Using the "#" in shell is surprisingly useful. Some of the uses I found: a) As a visible copy buffer in shell history (caveat: do not use for passwords :-) b) To build complex commands until ready then hit the HOME, DEL, ENTER keys to run it c) Placing reference data into shell history (search for tags with CTRL-R TAGNAME) d) Putting aside a "work in progress" command to focus on another task (HOME # ENTER) Show Sample Output

    # indicates a comment in shell
    mpb · 2009-03-16 23:15:33 0
  • If you're a moron like me, sometimes your fingers get away from you and you, for example, enter your password when you're already authenticated to ssh-agent, sudo, etc., and your password ends up in shell history. Here's how to get it out. Show Sample Output

    history -d
    sud0er · 2009-04-27 20:19:09 3

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: