在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
当前回答
这很简单。编辑文件.profile (vi, nano, Sublime Text或其他文本编辑器)文件。你可以在~/目录(用户目录)中找到它,并像这样设置:
export MY_VAR=[your value here]
Java home的示例:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/current
保存它并返回到终端。
你可以用:
source .profile
或者关闭并打开终端窗口。
其他回答
嗯,我不确定/etc/paths和~/. macosx /environment。plist文件。那些是新的。
但是使用Bash时,您应该知道每次新的shell调用都会执行.bashrc 并且.bash_profile只在启动时执行一次。
我不知道在Mac OS x上这种情况发生的频率有多高。我想随着windows系统启动一切,这种区别已经消失了。
就我个人而言,我通过创建一个.bashrc文件来消除困惑,其中包含我需要的所有内容,然后执行:
ln -s .bashrc .bash_profile
在追逐环境变量首选项窗格后,发现链接已经损坏,在苹果网站上搜索似乎表明他们已经忘记了它……我开始回到难以捉摸的发射过程。
在我的系统(Mac OS X 10.6.8)上,似乎在环境中定义的变量。plist被可靠地导出到从Spotlight启动的应用程序(通过launchd)。我的问题是,这些变量没有被导出到终端中的新bash会话。也就是说,我的问题与这里描述的相反。
注:环境。如前所述,plist看起来像JSON,而不是XML
我可以通过编辑~/MacOSX/environment让Spotlight应用程序看到变量。plist和 我能够通过在我的.profile文件中添加以下内容将相同的vars强制到一个新的Terminal会话中:
eval $(launchctl export)
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.
不管“蹩脚”与否,如果您将这些更改限制在较小的范围内,您将拥有一个更加稳定的系统。
对于单个用户修改,使用~/。你列出的人的简介。下面的链接解释了Bash何时读取不同的文件。
http://telin.ugent.be/~slippens/drupal/bashrc_and_others
如果你想为gui应用程序设置环境变量,你需要~/. macosx /environment。plist文件