在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)。
当前回答
更新(2017-08-04)
从macOS 10.12.6 (Sierra)开始,这个方法似乎已经停止在Apache httpd上工作(对于系统和launchctl配置的用户选项)。其他项目似乎没有受到影响。可以想象,这是httpd中的一个bug。
原来的答案
这涉及到OS X 10.10+(特别是10.11+由于无根模式,其中/usr/bin不再可写)。
我在很多地方读到过,使用launchctl setenv PATH <new PATH >来设置PATH变量由于OS X中的一个错误而不起作用(从个人经验来看似乎是正确的)。我发现还有另一种方法可以为非shell启动的应用程序设置PATH:
sudo launchctl config user path <new path>
这个选项记录在launchctl手册页中:
config system | user parameter value Sets persistent configuration information for launchd(8) domains. Only the system domain and user domains may be configured. The location of the persistent storage is an implementation detail, and changes to that storage should only be made through this subcommand. A reboot is required for changes made through this subcommand to take effect. [...] path Sets the PATH environment variable for all services within the target domain to the string value. The string value should conform to the format outlined for the PATH environment variable in environ(7). Note that if a service specifies its own PATH, the service-specific environment variable will take precedence. NOTE: This facility cannot be used to set general environment variables for all services within the domain. It is intentionally scoped to the PATH environment vari- able and nothing else for security reasons.
我已经确认这与从Finder(使用getenv获取PATH)开始的GUI应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。
其他回答
不要期望~/.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的信息。
我认为OP正在寻找一个简单的,类似windows的解决方案。
给你:
http://www.apple.com/downloads/macosx/system_disk_utilities/environmentvariablepreferencepane.html
布鲁诺正在正确的轨道上。我已经做了大量的研究,如果你想设置在所有GUI应用程序中可用的变量,你唯一的选择是/etc/launchd.conf。
请注意环境。plist不适用于通过Spotlight启动的应用程序。这是由史蒂夫·塞克斯顿记录的。
Open a terminal prompt Type sudo vi /etc/launchd.conf (note: this file might not yet exist) Put contents like the following into the file # Set environment variables here so they are available globally to all apps # (and Terminal), including those launched via Spotlight. # # After editing this file run the following command from the terminal to update # environment variables globally without needing to reboot. # NOTE: You will still need to restart the relevant application (including # Terminal) to pick up the changes! # grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl # # See http://www.digitaledgesw.com/node/31 # and http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x/ # # Note that you must hardcode the paths below, don't use environment variables. # You also need to surround multiple values in quotes, see MAVEN_OPTS example below. # setenv JAVA_VERSION 1.6 setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home setenv GROOVY_HOME /Applications/Dev/groovy setenv GRAILS_HOME /Applications/Dev/grails setenv NEXUS_HOME /Applications/Dev/nexus/nexus-webapp setenv JRUBY_HOME /Applications/Dev/jruby setenv ANT_HOME /Applications/Dev/apache-ant setenv ANT_OPTS -Xmx512M setenv MAVEN_OPTS "-Xmx1024M -XX:MaxPermSize=512m" setenv M2_HOME /Applications/Dev/apache-maven setenv JMETER_HOME /Applications/Dev/jakarta-jmeter Save your changes in vi and reboot your Mac. Or use the grep/xargs command which is shown in the code comment above. Prove that your variables are working by opening a Terminal window and typing export and you should see your new variables. These will also be available in IntelliJ IDEA and other GUI applications you launch via Spotlight.
这很简单。编辑文件.profile (vi, nano, Sublime Text或其他文本编辑器)文件。你可以在~/目录(用户目录)中找到它,并像这样设置:
export MY_VAR=[your value here]
Java home的示例:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/current
保存它并返回到终端。
你可以用:
source .profile
或者关闭并打开终端窗口。
更新(2017-08-04)
从macOS 10.12.6 (Sierra)开始,这个方法似乎已经停止在Apache httpd上工作(对于系统和launchctl配置的用户选项)。其他项目似乎没有受到影响。可以想象,这是httpd中的一个bug。
原来的答案
这涉及到OS X 10.10+(特别是10.11+由于无根模式,其中/usr/bin不再可写)。
我在很多地方读到过,使用launchctl setenv PATH <new PATH >来设置PATH变量由于OS X中的一个错误而不起作用(从个人经验来看似乎是正确的)。我发现还有另一种方法可以为非shell启动的应用程序设置PATH:
sudo launchctl config user path <new path>
这个选项记录在launchctl手册页中:
config system | user parameter value Sets persistent configuration information for launchd(8) domains. Only the system domain and user domains may be configured. The location of the persistent storage is an implementation detail, and changes to that storage should only be made through this subcommand. A reboot is required for changes made through this subcommand to take effect. [...] path Sets the PATH environment variable for all services within the target domain to the string value. The string value should conform to the format outlined for the PATH environment variable in environ(7). Note that if a service specifies its own PATH, the service-specific environment variable will take precedence. NOTE: This facility cannot be used to set general environment variables for all services within the domain. It is intentionally scoped to the PATH environment vari- able and nothing else for security reasons.
我已经确认这与从Finder(使用getenv获取PATH)开始的GUI应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。