Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  n.st May 8 at 16:34
    
The point is do everything in a single command –  RSFalcon7 May 8 at 17:14

3 Answers 3

up vote 2 down vote accepted

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|improve this answer
1  
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). –  derobert Mar 28 at 17:59
    
@derobert I solved some expansion problems and applied the alias trick you told me. Thanks a lot –  RSFalcon7 May 8 at 17:38

My "two keystroke solution" (one letter and the return key) is to have this alias set up:

alias a='. ~/.bash_aliases'

Then whenever I update my .bash_aliases file I just type areturn

One step further is maintain them across machines, using github:

bup () { [ $# = 1 ] && { cp -v ~/$1 ~/Dropnot/setups; cd ~/Dropnot/setups; git fetch; git merge origin/master; git add $1; git commit -m"$1 update"; git push origin master; cp -v $1 ~; cd -; } || echo "Error - no filename passed!";}

Usage: bup [file] # file must be in ~, is usually a dot file]

share|improve this answer

There are several files that run when you login to your shell including ~/.bash_profile and possibly ~/.bashrc and ~/.bash_aliases if those are referenced in your ~/.bash_profile file. To have an alias available every time you login, just put the command that creates the alias in one of those files.

http://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

share|improve this answer
    
I know that! The point is to make it permanent without resourcing any file –  RSFalcon7 Mar 28 at 17:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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