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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

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

其他回答

我认为OP正在寻找一个简单的,类似windows的解决方案。

给你:

http://www.apple.com/downloads/macosx/system_disk_utilities/environmentvariablepreferencepane.html

嗯,我不确定/etc/paths和~/. macosx /environment。plist文件。那些是新的。

但是使用Bash时,您应该知道每次新的shell调用都会执行.bashrc 并且.bash_profile只在启动时执行一次。

我不知道在Mac OS x上这种情况发生的频率有多高。我想随着windows系统启动一切,这种区别已经消失了。

就我个人而言,我通过创建一个.bashrc文件来消除困惑,其中包含我需要的所有内容,然后执行:

ln -s .bashrc .bash_profile

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

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