2

I just found frequently I decide to add an alias for longer time use at the same time use it now. So I have to type the same thing twice, first in current bash session then .bashrc.

  • 3
    First, put it in the .bashrc, then source ~/.bashrc. – DopeGhoti Jan 9 '14 at 19:48
  • 1
    I have this at the top of my ~/.bash_aliases: alias realias='source ~/.bash_aliases' – Anthon Jan 9 '14 at 20:12
3

Define this function (say, in your .bashrc):

function permAlias {
    alias "$@"   # set the alias(es) in this session
    printf 'alias %q\n' "$@" >> ~/.bash_aliases  # set it for all sessions
}

Then use it the same way you would make a normal alias:

% permAlias foo='/path/to/command -some --options=here'

Note: This isn't the most robust solution in the universe. It will probably break under all sorts of different use cases. But it will work for simple things.

| improve this answer | |
  • Your .bash_aliases will keep growing if you tweak or otherwise shadow existing aliases, but other than that, this is going to work well in typical situations (after a few quoting tweaks). – Gilles 'SO- stop being evil' Jan 10 '14 at 1:04
  • Works wonders most of time! – NathaneilCapital Jan 13 '14 at 5:44

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.