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
}
Related videos on Youtube
Author by
RSFalcon7
Updated on September 18, 2022Comments
-
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 about 10 yearsYou can use
.
instead ofsource
and press [Alt]+[.] to insert the last argument of the previous command, so you can do the equivalent ofsource ~/.bash_aliases.sh
in just four keystrokes without the need for any custom functions. -
RSFalcon7 about 10 yearsThe point is do everything in a single command
-
-
RSFalcon7 over 10 yearsI know that! The point is to make it permanent without resourcing any file
-
derobert over 10 yearsFYI: 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 usingecho -e
there; you want plainecho
(if you don't want to switch toalias
for some reason). -
RSFalcon7 about 10 years@derobert I solved some expansion problems and applied the alias trick you told me. Thanks a lot