欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tools:How do I create a permanent Bash alias?

程序员文章站 2022-03-02 15:30:13
...

 

How do I create a permanent Bash alias?

There are a lot of ways to create an alias. The most used ways are:

  1. Add aliases directly in your ~/.bashrc file

    For example: append these line to ~/.bashrc file

    alias ll='ls -l'
    alias rm='rm -i'

    Next time (after you have logged out/in, or done . ~/.bashrc) when you type rm the rm -icommand will be executed.

  2. 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'

shareimprove this answer

edited Feb 14 at 16:44

tools:How do I create a permanent Bash alias?

Dan

7,38055 gold badges4747 silver badges7474 bronze badges

answered Dec 15 '10 at 8:21

tools:How do I create a permanent Bash alias?

aneeshep

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

add a comment