在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)。
当前回答
我的个人实践是.bash_profile。我在这里添加路径并附加到Path变量,
GOPATH=/usr/local/go/bin/
MYSQLPATH=/usr/local/opt/mysql@5.6/bin
PATH=$PATH:$GOPATH:$MYSQLPATH
之后,我可以有个人的路径通过echo$ GOPATH, echo$MYSQLPATH或所有通过echo$ Path。
其他回答
有时候,前面所有的答案都不适用。如果你想在Eclipse或IntelliJ IDEA中访问一个系统变量(如M2_HOME),在这种情况下,唯一适合我的是:
首先(步骤1)编辑/etc/launchd.conf以包含如下一行:“setenv VAR value”,然后(步骤2)重新启动。
简单地修改.bash_profile将不起作用,因为在OS X中应用程序不会像在其他Unix中那样启动;它们不继承父函数的壳变量。其他的修改都不管用了,原因我也不知道。也许有人可以解释一下。
更新(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应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。
很简单:
编辑~ /。对变量进行配置并按如下方式放置
$ vim ~/.profile
在文件中放置:
MY_ENV_VAR =值
保存(:wq) 重启终端(退出并重新打开) 确保这一切都没问题:
$MY_ENV_VAR 美元的价值
不要期望~/.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的信息。
简单又快速地做了这个。首先创建一个~/。Bash_profile来自终端:
touch .bash_profile
then
open -a TextEdit.app .bash_profile
add
export TOMCAT_HOME=/Library/Tomcat/Home
保存文档,您就完成了。