tools:How do I create a permanent Bash alias?
How do I create a permanent Bash alias?
There are a lot of ways to create an alias. The most used ways are:
-
Add aliases directly in your
~/.bashrc
fileFor example: append these line to
~/.bashrc
filealias ll='ls -l' alias rm='rm -i'
Next time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed. -
The second method lets you make a separate aliases file, so you won't have to put them in
.bashrc
, but to a file of your choice. First, edit your~/.bashrc
file and add the following lines if they don't already exist, or uncomment them if they do:if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
Save it and close the file. After that, all you have to do is create a
~/.bash_aliases
file and add your aliases there, with the same format specified in the first method.Contents of my
~/.bash_aliases
file:alias cs='cd;ls'
7,38055 gold badges4747 silver badges7474 bronze badges
answered Dec 15 '10 at 8:21
23.1k1212 gold badges5656 silver badges7474 bronze badges
-
71
+1 for using
~/.bash_aliases
. – ændrük Jun 10 '11 at 5:48 -
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->
alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you. – erm3nda Apr 27 '17 at 12:08 -
somehow after I added the alias such as
alias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why? – Sajuuk Mar 22 '18 at 8:43 -
2
By default
~/.bashrc
contains inclusion for~/.bash_aliases
, no need to edit it. – Jaakko May 14 '18 at 9:49 -
1
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy. – Jose May 30 '18 at 18:10
上一篇: 不可用端口
下一篇: HttpClient超时区别