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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

$PATH变量也受制于path_helper,它反过来利用/etc/paths文件和/etc/paths.d中的文件

更详细的描述可以在PATH和Leopard(2008-11)的其他环境问题中找到。

其他回答

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文件

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

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

直到和包括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。

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

更新(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应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。