4

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.

2 Answers2

4

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.

Eugene S
  • 3,484
  • 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 '12 at 07: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 '12 at 08:14
3

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
        fi
}

trap prepend_fixed_history EXIT
Anil
  • 725