Setting environment variables in Yosemite

前端 未结 4 1855
时光说笑
时光说笑 2021-01-13 04:42

What is the proper way to modify environment variables like PATH in Yosemite?

This is this question Setting environment variables in OS X? but specifically for yose

4条回答
  •  半阙折子戏
    2021-01-13 05:23

    What shell are you using? I'm assuming you're using the default Bash shell. There's also Csh, Ksh, and Zsh.

    The Terminal.app on Mac OS X by default starts a new login shell each time a window is open. This means that $HOME/.bash_profile or $HOME/profile is always executed when you open a new terminal window. You can set particular defaults in here. For example, I set PS1 and set -o vi.

    NOTE: This may not be the case if you're using other Terminal apps like xterm. These open new terminal windows as just new shells. This means that you may not see the changes made in .bash_profile until you log out and log back in.

    You can try editing $HOME/.bashrc and see if that helps.

    What about other shells?

    If you're using Kornshell (ksh), you need to edit the $HOME/profile and not $HOME/.bash_profile. If you're using Zshell (zsh), you're on your own. It's too wacky to describe here. Read the manpage for zsh and search for ZDOTDIR.

    When you run a shell script, the $HOME/.bashrc is executed. Most people put something like this in their .bash_profile, so their .bashrc settings are included in a new terminal window:

    [[ -x $HOME/.bashrc ]] && source "$HOME/.bashrc"
    

    Some people set things they want to be set when they run a shell script, for example export $PS4="\$LINENO> ".

    The $PATH is a bit different. You can set it in .bash_profile (I would not set it in .bashrc), But, Mac OS X has an automated why on how systemwide paths are set. A file called /etc/paths is used to set the default path for all users using either Bash or Kornshell via the /usr/libexec/path_helper program.

    On my Mac, I set my $PATH to:

    /usr/local/bin:/usr/share/bin:/bin:/usr/bin:/usr/sbin:/sbin:$HOME/bin
    

    When I install programs, I usually install them under /opt when possible. Then, I link their binaries (where ever they're placed) to /usr/local/bin. This way, I don't have to keep building my PATH. Plus, it allows me to override system defaults. For example, /usr/bin/git is at 1.9.3. while my installed /usr/local/bin/git is at version 2.2.1.

    One thing you should not do is modify /etc/profile because changes there may be replaced on OS X upgrades.

提交回复
热议问题