By default bash loads lines from ~/.bash_history to history. Is it possible to add custom file to be also loaded? I want to inject file containing commands I frequently use and access them it via built-in search.

  • 2
    Typically people add aliases for frequently used commands...
    – HBruijn
    May 28 '15 at 18:19
  • That's great. But that's not the case - I use aliases for commands I use constantly but I have a lots of commands that have big number of arguments that I want to modify based on circumstances.
    – rgtk
    May 28 '15 at 18:28
  • 4
    You would have to cron out some kind of file modification, which sounds rather crazy and may not work. Just make your long commands a shell script that accepts arguments and drop it in /usr/local/bin or something
    – Wesley
    May 28 '15 at 18:37
  • If you use them frequently, wouldn't they be in your history anyway?
    – Cameron Kerr
    May 29 '15 at 8:48
  • Declare functions in .bashrc? May 29 '15 at 11:36

I keep it simple with an alias h='history|grep' loaded into my Bash profile.

So my workflow is h command, for example: h hpssa

#  h hpssa
  202  05-28-2015 11:54:33  hpssacli
  217  05-28-2015 11:54:33  hpssa -start
  225  05-28-2015 11:54:33  hpssacli -stop
  226  05-28-2015 11:54:33  hpssa -stop
  228  05-28-2015 11:54:33  hpssa -start

If I want to run "hpssa -stop", I'd simply type !226

This is just my approach, but maybe you could modify how you're recalling history items. I don't think it makes sense to actually inject data into the history file.

  • nice trick, stealing that one
    – wim
    May 28 '15 at 23:55
  • Suggest you may want to have a look at what Ctrl-R does for searching back in your history. Prepare to have your mind blown...
    – Cameron Kerr
    May 29 '15 at 8:47
  • 2
    @CameronKerr Have a look at the answer just below this one (serverfault.com/a/695115/120438 ).The downside to using Ctrl+R in this particular case is that it gives you the relevant commands one at a time, while ewwhite's approach returns a list of all relevant commands.
    – Jenny D
    May 29 '15 at 8:54
  • Although this is a great trick, it doesn't answer the question at all. I fail to understand as to how this is the highest voted answer. My usecase is that I have a docker image which keeps losing it's history when I exit it. I thought it'd be nice if I could pass a history file via the command line to docker run ...
    – kumarharsh
    Jan 12 '17 at 6:44
  • @kumar_harsh Hard to please.
    – ewwhite
    Jan 12 '17 at 7:03

I used to be very reliant on Ctrl-R (reverse-search-history Readline command) which searches backward through the history to retrieve previously entered long commands but I now prefer to use aliases and shell scripts / functions. Anyhow, the Bash history builtin command can be used to load a list of custom commands into your current history.

history -r [filename]

From the bash man page:

Read the contents of the history file and append them to the current history list.

Start-up automation

I just tried launching this command automatically by adding it as the last line in my .bashrc. However, the commands from my custom history file were not available when I started a new shell. It seems that the shell history is only loaded from the default .bash_history file: after the start-up files have been processed and just before the prompt is presented for the interactive shell. Unfortunately, it seems there isn't an easy way to automate this.


As @AnthonyGeoghegan observed, the history is loaded after startup files are processed. That offers a different way to pre-load the desired history in a startup file:

mv .bash_history .bash_history.tmp
cat .extra_hist .bash_history.tmp > .bash_history
# or swap them, to put the extra stuff at the end of the history, for
# quicker ^R access.

Note that this approach could result in multiple copies of your special commands being added to your .bash_history, over time. You could wrap your special commands with comment lines, so you can easily find and remove them (via, e.g., sed) as part of filling the .bash_history file. Something like:

cp .extra_hist .bash_history
sed '/: remove from here/,/:remove to here/d' .bash_history >> .bash_history

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.