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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

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

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

其他回答

在Mac OS上设置PATH环境变量

打开终端程序(默认在应用程序/实用程序文件夹中)。运行以下命令

touch ~/.bash_profile; open ~/.bash_profile

这将在默认文本编辑器中打开文件。

以Android SDK为例:

你需要添加路径到你的Android SDK平台工具和工具目录。在我的例子中,我将使用“/Development/android-sdk-macosx”作为SDK的安装目录。增加如下一行:

export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools

保存文件并退出文本编辑器。执行.bash_profile更新PATH:

source ~/.bash_profile

现在,每次你打开终端程序,你的路径将包括Android SDK。

iOS上的所有神奇之处都是使用source和文件,在这里导出环境变量。

例如:

你可以像这样创建一个文件:

export bim=fooo
export bom=bar

保存为bimbom。Env,然后做source ./bimbom.ev。 Voilá,你有你的环境变量。

检查它们:

echo $bim

简单又快速地做了这个。首先创建一个~/。Bash_profile来自终端:

touch .bash_profile

then

open -a TextEdit.app .bash_profile

add

export TOMCAT_HOME=/Library/Tomcat/Home

保存文档,您就完成了。

不要期望~/.launchd.conf能够工作

launchctl的手册页说它从来没有工作过:

已弃用和删除的功能 Launchctl不再具有交互模式,也不再接受来自stdin的命令。/etc/launchd.conf文件不再用于在早期启动时运行子命令;出于安全考虑,该功能已被删除。虽然有文档表明在设置用户会话之前会查询$HOME/.launchd.conf,但这个功能从未实现过。

如何为Spotlight启动的新进程设置环境(不需要重新启动)

您可以使用launchctl setenv设置launchd(以及从Spotlight开始的任何东西)所使用的环境。例如,设置路径:

launchctl setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

或者如果你想在。bashrc或类似文件中设置你的路径,那么在launchd中镜像它:

PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
launchctl setenv PATH $PATH

没有必要重新启动,但如果你想让应用程序接受改变的环境,就需要重新启动应用程序。

这包括已经在终端下运行的任何shell。如果你在那里,你可以更直接地设置环境,例如export PATH=/opt/local/bin:/opt/local/sbin:$PATH for bash或zsh。

重启后如何保持更改

新方法(自10.10 Yosemite起)

使用launchctl config用户路径/bin:/usr/bin:/mystuff。更多信息请参见man launchctl。

以前的方法

这个答案顶部的launchctl手册页引用说,出于安全原因,这里描述的特性(在引导时读取/etc/launchd.conf)被删除了,因此ymmv. conf。

为了在重启后保持更改,你可以在/etc/launchd.conf中设置环境变量,如下所示:

setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

重启时自动执行Launchd.conf。

如果你想让这些改变现在生效,你应该使用这个命令重新处理launchd.conf(感谢@mklement的提示!)

egrep -v '^\s*#' /etc/launchd.conf | launchctl

你可以通过命令man launchctl找到更多关于launchctl以及它如何加载launchd.conf的信息。

对于Bash,尝试将环境变量添加到文件/etc/profile中,以使它们对所有用户可用。不需要重新启动,只需启动一个新的终端会话。