How to create a permanent alias without resourcing the .bashrc-like file?

5,361

Here is my workaround, the function palias (aka permanent alias):

function palias ()  {

    if [ $# -ne "2" ] ; then
        error "Usage: palias short-alias \'long-alias\'"
        return
    fi

    alias $1="$2"
    alias $1 >> ~/.bash_aliases
}
Share:
5,361

Related videos on Youtube

RSFalcon7
Author by

RSFalcon7

Updated on September 18, 2022

Comments

  • RSFalcon7
    RSFalcon7 almost 2 years

    I really like bash aliases, it annoys me that every time I want to add a new alias I have type two commands:

    echo "alias \"short-cmd\"='long-command'" >> ~/.bash_aliases
    source ~/.bash_aliases
    

    Any way I can create an alias permanently with a single command?

    • n.st
      n.st about 10 years
      You can use . instead of source and press [Alt]+[.] to insert the last argument of the previous command, so you can do the equivalent of source ~/.bash_aliases.sh in just four keystrokes without the need for any custom functions.
    • RSFalcon7
      RSFalcon7 about 10 years
      The point is do everything in a single command
  • RSFalcon7
    RSFalcon7 over 10 years
    I know that! The point is to make it permanent without resourcing any file
  • derobert
    derobert over 10 years
    FYI: Running alias without a definition will show the current definition. You can use this to make sure the quoting works right—e.g., your current method fails if the definition contains single-quote. alias "$1" >> ~/.bash_aliases does not. I suggest switching (in both your answer and on your systems). Also, you shouldn't be using echo -e there; you want plain echo (if you don't want to switch to alias for some reason).
  • RSFalcon7
    RSFalcon7 about 10 years
    @derobert I solved some expansion problems and applied the alias trick you told me. Thanks a lot