在OS X中修改环境变量如PATH的正确方法是什么?

我看了谷歌一点,找到了三个不同的文件进行编辑:

/etc/paths ~ / . profile ~ / tcshrc

我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?

我运行的是OS X v10.5 (Leopard)。


当前回答

做的事: vim ~ / . bash_profile 文件可能不存在(如果不存在,您可以直接创建它)。 输入并保存文件: 导出路径= $路径:YOUR_PATH_HERE 运行 源~ / . bash_profile

其他回答

直到和包括OS X v10.7 (Lion),你可以设置它们:

~/.MacOSX/environment.plist

See:

https://developer.apple.com/legacy/library/qa/qa1067/_index.html https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html

对于终端中的PATH,您应该能够在.bash_profile或.profile中设置(尽管您可能必须创建它)

对于OS X v10.8 (Mountain Lion)及以上版本,您需要使用launchd和launchctl。

来自单一来源的命令行和GUI应用程序的解决方案(适用于Mac OS X v10.10 (Yosemite)和Mac OS X v10.11 (El Capitan))

让我们假设在~/中有环境变量定义。Bash_profile如下所示:

export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
export GOPATH="$HOME/go"
export PATH="$PATH:/usr/local/opt/go/libexec/bin:$GOPATH/bin"
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

我们需要一个启动代理,它将在每次登录时运行,并随时按需将这些变量加载到用户会话。我们还需要一个shell脚本来解析这些定义并构建由代理执行的必要命令。

在~/Library/LaunchAgents/目录下创建一个后缀为plist的文件(例如osx-env-sync.plist),内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>osx-env-sync</string>
  <key>ProgramArguments</key>
  <array>
    <string>bash</string>
    <string>-l</string>
    <string>-c</string>
    <string>
      $HOME/.osx-env-sync.sh
    </string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

-l参数在这里很重要;使用登录shell执行shell脚本是必要的,以便~/。Bash_profile是在执行脚本之前首先获取的。

现在,shell脚本。在~/.osx-env-sync.sh中创建以下内容:

grep export $HOME/.bash_profile | while IFS=' =' read ignoreexport envvar ignorevalue; do
  launchctl setenv "${envvar}" "${!envvar}"
done

确保shell脚本是可执行的:

chmod +x ~/.osx-env-sync.sh

现在,加载当前会话的启动代理:

launchctl load ~/Library/LaunchAgents/osx-env-sync.plist

(重新)启动一个GUI应用程序并验证它可以读取环境变量。

设置是持久的。它将在重新启动和重新登录后仍然有效。

在初始设置之后(您刚刚完成了),如果您想在~/中反映任何更改。Bash_profile再次到您的整个环境,重新运行launchctl load…命令不会执行你想要的;相反,你会得到如下警告:

< $ HOME > /图书馆/ LaunchAgents / osx-env-sync。plist:操作已经在进行中

为了在不退出/登录的情况下重新加载环境变量,请执行以下操作:

launchctl unload ~/Library/LaunchAgents/osx-env-sync.plist
launchctl load ~/Library/LaunchAgents/osx-env-sync.plist

最后,确保重新启动已经运行的应用程序(包括Terminal.app),让它们知道这些更改。

我还将这里的代码和解释推送到GitHub项目:osx-env-sync。

我希望这将是最终的解决方案,至少对于最新版本的OS X (Yosemite & El Capitan)来说是这样。

另一个免费、开源的Mac OS X v10.8 (Mountain Lion)首选项窗格/环境。plist解决方案是EnvPane。

EnvPane的源代码可在GitHub。EnvPane看起来具有与rcenvirenvironment相当的功能,然而,它似乎可以立即更新其存储的变量,即不需要重新启动或登录,这是受欢迎的。

如开发商所述:

EnvPane is a preference pane for Mac OS X 10.8 (Mountain Lion) that lets you set environment variables for all programs in both graphical and terminal sessions. Not only does it restore support for ~/.MacOSX/environment.plist in Mountain Lion, it also publishes your changes to the environment immediately, without the need to log out and back in. <SNIP> EnvPane includes (and automatically installs) a launchd agent that runs 1) early after login and 2) whenever the ~/.MacOSX/environment.plist changes. The agent reads ~/.MacOSX/environment.plist and exports the environment variables from that file to the current user's launchd instance via the same API that is used by launchctl setenv and launchctl unsetenv.

免责声明:我与开发者或他/她的项目没有任何关系。

附注:我喜欢这个名字(听起来像“Ends Pain”)。

虽然这里的答案并不是“错误的”,但我还要补充一点:永远不要在OS X中改变影响“所有进程”的环境变量,甚至在shell之外,也不要影响给定用户交互运行的所有进程。

In my experience, global changes to environment variables like PATH for all processes are even more likely to break things on OS X than on Windows. Reason being, lots of OS X applications and other software (including, perhaps especially, components of the OS itself) rely on UNIX command-line tools under the hood, and assume the behavior of the versions of these tools provided with the system, and don't necessarily use absolute paths when doing so (similar comments apply to dynamically-loaded libraries and DYLD_* environment variables). Consider, for instance, that the highest-rated answers to various Stack Overflow questions about replacing OS X-supplied versions of interpreters like Python and Ruby generally say "don't do this."

OS X is really no different than other UNIX-like operating systems (e.g., Linux, FreeBSD, and Solaris) in this respect; the most likely reason Apple doesn't provide an easy way to do this is because it breaks things. To the extent Windows isn't as prone to these problems, it's due to two things: (1) Windows software doesn't tend to rely on command-line tools to the extent that UNIX software does, and (2) Microsoft has had such an extensive history of both "DLL hell" and security problems caused by changes that affect all processes that they've changed the behavior of dynamic loading in newer Windows versions to limit the impact of "global" configuration options like PATH.

不管“蹩脚”与否,如果您将这些更改限制在较小的范围内,您将拥有一个更加稳定的系统。

要简洁明了地说明每个文件的用途

~ /。概要文件是每次终端源。App上线 ~ /。bashrc是“传统上”设置Bash环境的所有导出语句的地方 /etc/paths是Mac OS中的主文件,包含为所有用户构建PATH环境变量的默认路径列表 /etc/paths.D /包含包含其他搜索路径的文件

非终端程序不继承您的终端所继承的系统范围的PATH和MANPATH变量!要为特定用户启动的所有进程设置环境,从而使MacOSX GUI应用程序可以使用环境变量,这些变量必须在~/. macosx /environment中定义。plist (Apple Technical Q&A QA1067)

使用以下命令行同步您的环境。Plist /etc/paths:

defaults write $HOME/.MacOSX/environment PATH "$(tr '\n' ':' </etc/paths)"