Skip to content

Persistent bash history > Load into current history

After setting up a persistent bash history, I looked for ways to use it efficiently. I wrote a function that loads the persistent history into the current terminal history, so it can be used immediately.

Here is the code, which you need to add to your ~/.bashrc file:

<pre>
load_persistent_history()
{
    tail -100 ~/.persistent_history | grep -oP '(?&lt;=\| ).+' &gt;&gt; ~/.bash_history.tmp
    history -r ~/.bash_history.tmp
    rm ~/.bash_history.tmp
}
</pre>

So, what exactly happens here?
First of all, reload the .bashrc script by typing source ~/.bashrc.
As you can see, the last 100 lines in the .persistent_history file are written to a temporary file. Then this file is loaded into the current history and deleted again. Whenever you type load_persistent_history into the bash, this code gets executed and the last 100 commands are accessible in your current terminal session.

Crazy hack, but damn useful!

Published inTips and Tricks

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *