在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)。
当前回答
嗯,我不确定/etc/paths和~/. macosx /environment。plist文件。那些是新的。
但是使用Bash时,您应该知道每次新的shell调用都会执行.bashrc 并且.bash_profile只在启动时执行一次。
我不知道在Mac OS x上这种情况发生的频率有多高。我想随着windows系统启动一切,这种区别已经消失了。
就我个人而言,我通过创建一个.bashrc文件来消除困惑,其中包含我需要的所有内容,然后执行:
ln -s .bashrc .bash_profile
其他回答
$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文件
这里有一个非常简单的方法来做你想做的事情。在我的例子中,它是让Gradle工作(Android Studio)。
打开终端。 执行如下命令: Sudo nano /etc/paths或Sudo vim /etc/paths 在提示时输入密码。 转到文件底部,输入要添加的路径。 点击Control + X退出。 输入“Y”保存修改后的缓冲区。 打开一个新的终端窗口,然后输入: 回声路径美元
您应该看到新路径被追加到path的末尾。
我从这篇文章中得到了这些细节:
添加到Mac OS X 10.8 Mountain Lion及以上版本的PATH中
简单又快速地做了这个。首先创建一个~/。Bash_profile来自终端:
touch .bash_profile
then
open -a TextEdit.app .bash_profile
add
export TOMCAT_HOME=/Library/Tomcat/Home
保存文档,您就完成了。
另一个免费、开源的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”)。