Mac OS X 如何设置 $PATH 的值?

Psy*_*nch 13 mac bash path macos

我对如何设置 $PATH 有一个基本的了解,但是是否有文档完整描述了 Mac OS 获取附加到 $PATH 的所有路径的位置?我知道诸如/etc/profile,/etc/paths和 之类的东西/etc/profile.d,但是还有其他脚本最终会影响 $PATH 的值吗?我对非登录 shell 和登录 shell ( .bashrc, .bash_profile)也不是很熟悉,但我知道基本的区别。

slh*_*hck 9

通常,您的 PATH 是由 shell 设置的。对于 Bash,手册中解释了所有内容。您也可以打开man bash并跳至该INVOCATION部分。

作为交互式登录 shell 调用,或使用 --login

当 Bash 作为交互式登录 shell 或作为带有 --login 选项的非交互式 shell 调用时,它首先从文件 /etc/profile 读取并执行命令(如果该文件存在)。读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,然后从第一个存在且可读的命令中读取和执行命令。

作为交互式非登录 shell 调用

当一个不是登录 shell 的交互式 shell 启动时,Bash 从 ~/.bashrc 读取并执行命令(如果该文件存在)。此外,在 OS X 中,还有path_helperwhich 读取内容/etc/paths.d并将其附加到您的路径中。

这里的关键是,在 OS X 上,终端默认打开一个登录 shell,而在 Linux 上,shell 通常作为非登录 shell 启动。Josh Staiger对登录与非登录 shell很好的解释

因此,基本上只有这两个三个可以设置路径:

  • /etc/profile(调用path_helper
  • /etc/paths/etc/paths.d(从 调用path_helper
  • 你的 shell 配置文件 ( .bash_profile)


Lri*_*Lri 8

/etc/paths/etc/paths.d/*中的路径通常PATHpath_helper添加。path_helper是从 运行的/etc/profile,因此它在 bash 作为交互式登录 shell 调用时运行,但在 bash 作为非登录 shell 或非交互式 shell 调用时不运行。

/etc/paths/usr/local/bin默认包含在末尾,默认/etc/paths.d/为空。

Terminal和iTerm 2默认打开新的shell作为登录shell,ssh到电脑时打开的shell也是登录shell。其他平台上的许多终端模拟器tmux,以及 Emacs 中的 shell 模式将新 shell 作为非登录 shell 打开。

我已将此行添加到/etc/launchd.conf

setenv PATH ~/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec:/usr/texbin

它改变了PATHroot launchd 进程的值。该值由所有其他进程继承,包括每个用户启动的进程。您可以/etc/launchd.conf通过重新启动或运行launchctl < /etc/launchd.conf; sudo launchctl < /etc/launchd.conf并重新启动进程来应用更改。

在 OS X 上,~/.profile以图形方式登录时不会读取。如果同时~/.bash_profile~/.profile存在,bash将不读~/.profile无论是。

~/.MacOSX/environment.plist 在 10.8 中停止工作。