在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)。
当前回答
这里有一个非常简单的方法来做你想做的事情。在我的例子中,它是让Gradle工作(Android Studio)。
打开终端。 执行如下命令: Sudo nano /etc/paths或Sudo vim /etc/paths 在提示时输入密码。 转到文件底部,输入要添加的路径。 点击Control + X退出。 输入“Y”保存修改后的缓冲区。 打开一个新的终端窗口,然后输入: 回声路径美元
您应该看到新路径被追加到path的末尾。
我从这篇文章中得到了这些细节:
添加到Mac OS X 10.8 Mountain Lion及以上版本的PATH中
其他回答
/etc/launchd.conf在OS X v10.10 (Yosemite)、OS X v10.11 (El Capitan)、macOS v10.12 (Sierra)或macOS v10.13 (High Sierra)中不使用。
从launchctl手册页:
/etc/launchd.conf file is no longer consulted for subcommands to run during early boot time;
this functionality was removed for security considerations.
这个Ask Different answer中描述的方法适用于我(重启后):从Dock或Spotlight启动的应用程序继承了我在~/Library/LaunchAgents/my.startup.plist中设置的环境变量。(在我的例子中,我需要将LANG设置为en_US。UTF-8,用于Sublime Text插件。)
这里有一个非常简单的方法来做你想做的事情。在我的例子中,它是让Gradle工作(Android Studio)。
打开终端。 执行如下命令: Sudo nano /etc/paths或Sudo vim /etc/paths 在提示时输入密码。 转到文件底部,输入要添加的路径。 点击Control + X退出。 输入“Y”保存修改后的缓冲区。 打开一个新的终端窗口,然后输入: 回声路径美元
您应该看到新路径被追加到path的末尾。
我从这篇文章中得到了这些细节:
添加到Mac OS X 10.8 Mountain Lion及以上版本的PATH中
有时候,前面所有的答案都不适用。如果你想在Eclipse或IntelliJ IDEA中访问一个系统变量(如M2_HOME),在这种情况下,唯一适合我的是:
首先(步骤1)编辑/etc/launchd.conf以包含如下一行:“setenv VAR value”,然后(步骤2)重新启动。
简单地修改.bash_profile将不起作用,因为在OS X中应用程序不会像在其他Unix中那样启动;它们不继承父函数的壳变量。其他的修改都不管用了,原因我也不知道。也许有人可以解释一下。
就像Matt Curtis给出的答案一样,我通过launchctl设置环境变量,但我将它包装在一个名为export的函数中,因此每当我像在.bash_profile中一样导出一个变量时,它也由launchctl设置。我是这样做的:
My .bash_profile consists solely of one line, (This is just personal preference.) source .bashrc My .bashrc has this: function export() { builtin export "$@" if [[ ${#@} -eq 1 && "${@//[^=]/}" ]] then launchctl setenv "${@%%=*}" "${@#*=}" elif [[ ! "${@//[^ ]/}" ]] then launchctl setenv "${@}" "${!@}" fi } export -f export The above will overload the Bash builtin "export" and will export everything normally (you'll notice I export "export" with it!), then properly set them for OS X app environments via launchctl, whether you use any of the following: export LC_CTYPE=en_US.UTF-8 # ~$ launchctl getenv LC_CTYPE # en_US.UTF-8 PATH="/usr/local/bin:${PATH}" PATH="/usr/local/opt/coreutils/libexec/gnubin:${PATH}" export PATH # ~$ launchctl getenv PATH # /usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin export CXX_FLAGS="-mmacosx-version-min=10.9" # ~$ launchctl getenv CXX_FLAGS # -mmacosx-version-min=10.9 This way I don't have to send every variable to launchctl every time, and I can just have my .bash_profile / .bashrc set up the way I want. Open a terminal window, check out your environment variables you're interested in with launchctl getenv myVar, change something in your .bash_profile/.bashrc, close the terminal window and re-open it, check the variable again with launchctl, and voilá, it's changed. Again, like the other solutions for the post-Mountain Lion world, for any new environment variables to be available for apps, you need to launch or re-launch them after the change.
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文件