在OS X中修改环境变量如PATH的正确方法是什么?

我看了谷歌一点,找到了三个不同的文件进行编辑:

/etc/paths ~ / . profile ~ / tcshrc

我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?

我运行的是OS X v10.5 (Leopard)。


当前回答

在Mountain Lion上,所有的/etc/paths和/etc/launchd.conf编辑都不起任何作用!

苹果的开发者论坛说:

“更改信息。.app本身的plist包含一个“lsenvirenvironment” 带有所需环境变量的字典。 ~ / .MacOSX /环境。不再支持Plist。”

所以我直接编辑了应用程序的信息。plist(右击“AppName. plist”)app”(在本例中是SourceTree),然后是“显示包内容”)。

我添加了一个新的键/字典对,称为:

<key>LSEnvironment</key>
<dict>
     <key>PATH</key>
     <string>/Users/flori/.rvm/gems/ruby-1.9.3-p362/bin:/Users/flori/.rvm/gems/ruby-1.9.3-p362@global/bin:/Users/flori/.rvm/rubies/ruby-1.9.3-p326/bin:/Users/flori/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:</string>
</dict>

(参见苹果的LaunchServicesKeys文档)

现在应用程序(在我的例子中是Sourcetree)使用给定的路径并使用Git 1.9.3:-)

PS:当然,您必须调整路径条目以满足您特定的路径需求。

其他回答

登录shell

/etc/profile

shell首先执行/etc/profile.文件中的命令具有根权限的用户可以设置这个文件,为运行Bash的用户建立系统范围内的默认特征。

.bash_profile
.bash_login
.profile

接下来shell查找~/。bash_profile、~ /。Bash_login和~/。配置文件(~/是您的主目录的简写),按此顺序执行它找到的第一个文件中的命令。您可以在其中一个文件中放置命令来覆盖/etc/profile中设置的默认值在虚拟终端上运行的shell不会执行这些文件中的命令。

.bash_logout

注销时,bash执行~/. conf中的命令。bash_logout文件。这个文件通常保存在会话结束后清理的命令,比如删除临时文件的命令。

交互式非登录shell

/etc/bashrc

虽然不是由bash直接调用,但许多~/。Bashrc文件调用/etc/bashrc.这种设置允许使用根权限的用户为非登录bash shell建立系统范围内的默认特征。

.bashrc

交互式非登录shell在~/. shell中执行命令。bashrc文件。(通常,登录shell的启动文件(如.bash_profile)将运行此文件,因此登录和非登录shell都将运行.bashrc中的命令。

因为.bashrc中的命令可能会被执行多次,并且子shell继承导出的变量,所以将添加到现有变量的命令放在.bash_profile文件中是一个好主意。

不要期望~/.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,尝试将环境变量添加到文件/etc/profile中,以使它们对所有用户可用。不需要重新启动,只需启动一个新的终端会话。

就像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.

简单又快速地做了这个。首先创建一个~/。Bash_profile来自终端:

touch .bash_profile

then

open -a TextEdit.app .bash_profile

add

export TOMCAT_HOME=/Library/Tomcat/Home

保存文档,您就完成了。