How do I create a Bash alias?

后端 未结 16 667
一个人的身影
一个人的身影 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:07

    MacOS Catalina and Above

    Apple switched their default shell to zsh, so the config files include ~/.zshenv and ~/.zshrc. This is just like ~/.bashrc, but for zsh. Just edit the file and add what you need; it should be sourced every time you open a new terminal window:

    nano ~/.zshenv alias py=python

    Then do ctrl+x, y, then enter to save.

    This file seems to be executed no matter what (login, non-login, or script), so seems better than the ~/.zshrc file.

    High Sierra and earlier

    The default shell is bash, and you can edit the file ~/.bash_profile and add aliases:

    nano ~/.bash_profile alias py=python

    Then ctrl+x, y, and enter to save. See this post for more on these configs. It's a little better to set it up with your alias in ~/.bashrc, then source ~/.bashrc from ~/.bash_profile. In ~/.bash_profile it would then look like:

    source ~/.bashrc

提交回复
热议问题