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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

另一个免费、开源的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”)。

其他回答

来自单一来源的命令行和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)来说是这样。

2月2022日(MacOs 12+)

这里的解决方案应该在重新启动或应用程序重新启动后工作。

CLI

打开所选配置文件的CLI。

为bash打开~/.bash_profile 为zsh打开~/.zshrc

添加(或替换)

export varName=varValue 

(如果varValue中有空格-将其包装成")

确保重新启动命令行应用程序。

GUI

完成CLI步骤。 确保GUI应用程序关闭。 从命令行打开GUI应用程序。例如:

open /Applications/Sourcetree.app

(你也可以在.zshrc文件中使用这个命令的别名)

原则

Mac没有为所有上下文设置环境变量的配置选项。 避免更改用户配置文件之外的任何内容。

已经不管用了

(MacOS 12.1+) 编辑/etc/launchd.conf 带有plist后缀的XML文件

更新(2017-08-04)

从macOS 10.12.6 (Sierra)开始,这个方法似乎已经停止在Apache httpd上工作(对于系统和launchctl配置的用户选项)。其他项目似乎没有受到影响。可以想象,这是httpd中的一个bug。

原来的答案

这涉及到OS X 10.10+(特别是10.11+由于无根模式,其中/usr/bin不再可写)。

我在很多地方读到过,使用launchctl setenv PATH <new PATH >来设置PATH变量由于OS X中的一个错误而不起作用(从个人经验来看似乎是正确的)。我发现还有另一种方法可以为非shell启动的应用程序设置PATH:

sudo launchctl config user path <new path>

这个选项记录在launchctl手册页中:

config system | user parameter value Sets persistent configuration information for launchd(8) domains. Only the system domain and user domains may be configured. The location of the persistent storage is an implementation detail, and changes to that storage should only be made through this subcommand. A reboot is required for changes made through this subcommand to take effect. [...] path Sets the PATH environment variable for all services within the target domain to the string value. The string value should conform to the format outlined for the PATH environment variable in environ(7). Note that if a service specifies its own PATH, the service-specific environment variable will take precedence. NOTE: This facility cannot be used to set general environment variables for all services within the domain. It is intentionally scoped to the PATH environment vari- able and nothing else for security reasons.

我已经确认这与从Finder(使用getenv获取PATH)开始的GUI应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。

虽然这里的答案并不是“错误的”,但我还要补充一点:永远不要在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.

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

这里有两种类型的炮弹。

非登录:每次启动一个新的Bash副本时,.bashrc都会被重新加载 登录:.profile只有在登录或显式告诉Bash加载它并将其用作登录shell时才会被加载。

在这里,重要的是要理解在Bash中,.bashrc文件只能由交互式和非登录的shell读取,您会发现人们经常在.bash_profile中加载.bashrc以克服这一限制。

现在您已经有了基本的了解,让我们继续讨论我建议您如何设置它。

.profile:创建一个不存在的文件。把你的PATH设置放在那里。 .bashrc:如果不存在则创建。把你所有的别名和自定义方法都放进去。 .bash_profile:如果不存在则创建。把下面的东西放进去。

.bash_file:

#!/bin/bash
source ~/.profile # Get the PATH settings
source ~/.bashrc  # Get Aliases and Functions
#