在 MAC 上工作, 竟然遇到要更改 PATH 环境变量的情况. 比如有的 Java 工程是基于 JDK8 的, 有的是基于 JDK11 的, 所以经常要改这个变量. 那么我们看到的 PATH 环境变量是从哪里来的, 那些配置文件会改这些 PATH 值呢?
首先, 我们先看下当前的 PATH 变量:
~ xiatian$ echo $PATH
/usr/local//Cellar/curl/7.80.0_1/bin/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/Users/xiatian/work/tools:/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/
这里面有好多路径, 那么我们好奇当系统启动的过程中, 最初的 PATH 是什么呢?
MAC 上最初的 PATH 是从 /etc/paths
文件读的:
~ xiatian$ cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
然后, 它会把 /etc/path.d
里面的路径逐个添加到后面, 比如我们可以看到我的 wireshark 路径在上面的 PATH 里:
~ xiatian$ ls -l /etc/paths.d/
total 8
-rw-r--r-- 1 root wheel 43 Oct 19 2017 Wireshark
~ xiatian$ cat /etc/paths.d/Wireshark
/Applications/Wireshark.app/Contents/MacOS
- 在个人的 home 目录, 还有2个文件会影响 PATH 的值:
.bash_profile
& .bash_rc
. 他们的区别是: 当一个 shell 是 login shell 的时候, 它会先执行 .bash_profile
, 当是一个非 login 交互式 shell 的时候, 它执行 .bashrc
. 但是在 MAC 上, 默认都是 login shell, 所以应该都执行. 来源于这个问答. 我试过了自带的 Terminal 和 安装的 iterm, 发现我的 MAC 都不是这么玩的. 我本地是执行的 ~/.profile. 查看 bash 的 man, 你会在 INVOCATION section 看到具体的配置执行顺序:
~ xiatian$ man bash
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
所以, 只要仔细阅读 man bash
的内容, 你就知道 PATH 是怎么变化的啦.