/etc/profile /etc/bashrc ~/.bash_profile ~/.profile ~/.bashrc 为 Shell 启动配置文件。
macOS 下,这些文件不会在系统登录的时候被执行,只会在 Terminal.app 等终端程序启动相应 Shell 时被加载执行。
bash
当 bash 作为一个 interactive login shell 或者以 bash --login 启动时:
- 首先尝试执行
/etc/profile文件。 - macOS 中的
/etc/profile会调用/etc/bashrc。 - 接下来依次寻找
~/.bash_profile~/.bash_login~/.profile,执行第一个找到的文件。 - 当 login shell 退出时,尝试执行
~/.bash_logout。
当 bash 不作为 login shell 启动时:
- 尝试执行
~/.bashrc。
macOS 中的 ~/.bash_profile 会调用 ~/.profile 和 ~/.bashrc
sh
sh 会使用 /etc/profile 和 ~/.profile 文件
总结
macOS 下使用 bash 时,通常只需通过 ~/.bashrc 来设置环境变量即可。
如何全局设置 $PATH
我们需要看一下 $PATH 的构建过程:
- 加入
/etc/paths中的每一行 - 加入目录
/etc/paths.d/下每个文件的每一行 - shell 启动,执行
/etc/profile等文件中的export PATH=xxx:$PATH语句
所以只需要在 /etc/paths.d/ 目录下新建一个文件,并在其中添加指定的路径,即可将其添加到全局范围内的 $PATH 变量中。
经过测试,macOS 下通过
/etc/paths或/etc/paths.d/设置$PATH变量后,新开 shell 会立即生效,无需重启。