Mac OS X 如何設定 $PATH 的值?

Mac OS X 如何設定 $PATH 的值?

我對如何設定 $PATH 有基本的了解,但是有沒有完整描述 Mac OS 取得位置的文檔全部附加到 $PATH 的路徑?我知道諸如/etc/profile/etc/paths、 和 之類的東西/etc/profile.d,但是還有其他腳本最終影響 $PATH 的值嗎?我對非登入 shell 和登入 shell ( .bashrc.bash_profile) 也不是很熟悉,但我知道基本的差異。

答案1

通常,您的 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_helper它讀取 的內容/etc/paths.d並將其附加到您的路徑中。

這裡的關鍵是,在 OS X 上,終端機預設會開啟登入 shell,而在 Linux 上,shell 通常會作為非登入 shell 啟動。喬許·斯泰格有登入與非登入 shell 的一個很好的解釋

因此,基本上只有這兩個三個可以設定路徑:

  • /etc/profile(調用path_helper
  • /etc/paths/etc/paths.d(從 調用path_helper
  • 你的 shell 設定檔 ( .bash_profile)

答案2

/etc/paths和中的路徑/etc/paths.d/*通常PATH路徑助手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

PATH它更改root launchd 進程的值。該值由所有其他進程繼承,包括每個用戶啟動的進程。您可以/etc/launchd.conf透過重新啟動或運行launchctl < /etc/launchd.conf; sudo launchctl < /etc/launchd.conf並重新啟動進程來套用變更。

在 OS X 上,~/.profile當您以圖形方式登入時不會讀取。如果 和 都~/.bash_profile存在~/.profile,bash 也不讀取~/.profile

~/.MacOSX/environment.plist10.8 停止工作。

相關內容