Is there any way to store a command in bash history permanently? The actual scenario goes like this:-

I have a project in C and in order to configure my project, I have to run a very long command which is very difficult to remember. Once configured and build, I don't need that command for almost a month or so. Due to automatically deletion of old commands from the bash history, next time when I need that command, its not there in the history.

Note: Don't post solutions like store the command in a script file and execute script whenever required.

  • See if this posting helps <unix.stackexchange.com/questions/17574/…>
    – Kris srini
    Feb 22, 2012 at 6:53
  • 12
    Why would you not just put an alias in your .bashrc?
    – Psirus
    Feb 22, 2012 at 7:24
  • 3
    From a practical point of view, it's helpful that one stores such commands in a script. Or as an alias in .bashrc. If neither of these, then perhaps write it in a file, and copy-paste from there every time.
    – Barun
    Feb 22, 2012 at 8:15
  • 1
    You write that writing a script does not satisfy you, but don't give reasons for why you don't want this. Your motivation is therefore very unclear. Feb 22, 2012 at 11:00
  • 1
    If the command were in the history you could use reverse intelligent search to find it. This would be wicked convenient so you don't have to memorize all the aliases/scripts you may have set up.
    – Adam B
    Apr 12, 2013 at 0:34

2 Answers 2


You can just use alias for that purpose. To make your alias permanent you can add it to your ~/.bashrc file. For example you if you add alias list='ls -la' line, each time you will enter the list command in your shell, ls -la command will be actually executed. Note that if you just created your alias from command line without putting it to your bashrc file, this alias will get lost each time you re-enter your shell.

  • Great idea ;-) I'm interested wether that counts as "a solution like storing the command in a script file", and if so, what the reason for this requirement is.
    – Psirus
    Feb 22, 2012 at 7:57
  • I guess that he just meant that he didn't want to run an actual script which must be saved in a file. So I assume that the alias solution does count. This is if I understand the question correctly :)
    – Eugene S
    Feb 22, 2012 at 8:14

For your main problem I'd suggest that you have a text file with all your hairy commands listed out - like a scratch pad or something. Copy-Paste when required (as Barun as suggested).

If you absolutely want to do the history search for those commands, you could try prepending list of your important commands just when bash exits. Something on the lines of the following (its crude, I'm using it to illustrate):

Put the commands you don't want history file to forget in another file (say ~/fixed_hist). Add the following to your ~/.bashrc.

function prepend_fixed_history
        fixed_hist_c=$(wc -l ~/fixed_hist | awk '{print $1 }')
        head -${fixed_hist_c} ~/.bash_history | \
             diff - ~/fixed_hist >/dev/null 2>&1 
        if [ "$?" -ne "0" ]; then
           cat ~/fixed_hist ~/.bash_history > ~/.bash_history_new
           cp ~/.bash_history_new ~/.bash_history

trap prepend_fixed_history EXIT

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.