How do I create a Bash alias?

后端 未结 16 587
一个人的身影
一个人的身影 2020-11-28 17:38

I\'m on OSX and I need to put something like this, alias blah=\"/usr/bin/blah\" in a config file but I don\'t know where the config file is.

相关标签:
16条回答
  • 2020-11-28 18:26

    If you put blah="/usr/bin/blah" in your ~/.bashrc then you can use $blah in your login shell as a substitute for typing /usr/bin/blah

    0 讨论(0)
  • 2020-11-28 18:27

    You can add an alias or a function in your startup script file. Usually this is .bashrc, .bash_login or .profile file in your home directory.

    Since these files are hidden you will have to do an ls -a to list them. If you don't have one you can create one.


    If I remember correctly, when I had bought my Mac, the .bash_login file wasn't there. I had to create it for myself so that I could put prompt info, alias, functions, etc. in it.

    Here are the steps if you would like to create one:

    1. Start up Terminal
    2. Type cd ~/ to go to your home folder
    3. Type touch .bash_profile to create your new file.
    4. Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
    5. Type . .bash_profile to reload .bash_profile and update any alias you add.
    0 讨论(0)
  • 2020-11-28 18:30

    I think it's proper way:

    1) Go to teminal. open ~/.bashrc. Add if not exists

    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    

    2) open ~/.bash_aliases. If not exists: touch ~/.bash_aliases && open ~/.bash_aliases

    3) To add new alias rather
    - edit .bash_aliases file and restart terminal or print source ~/.bash_aliases
    - print echo "alias clr='clear'" >> ~/.bash_aliases && source ~/.bash_aliases where your alias is alias clr='clear'.

    4) Add line source ~/.bash_aliases to ~/.bash_profile file. It needs to load aliases in each init of terminal.

    0 讨论(0)
  • 2020-11-28 18:32
    cd /etc
    sudo vi bashrc
    

    Add the following like:

    alias ll="ls -lrt"
    

    Finally restart Terminal.

    0 讨论(0)
提交回复
热议问题