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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

在追逐环境变量首选项窗格后,发现链接已经损坏,在苹果网站上搜索似乎表明他们已经忘记了它……我开始回到难以捉摸的发射过程。

在我的系统(Mac OS X 10.6.8)上,似乎在环境中定义的变量。plist被可靠地导出到从Spotlight启动的应用程序(通过launchd)。我的问题是,这些变量没有被导出到终端中的新bash会话。也就是说,我的问题与这里描述的相反。

注:环境。如前所述,plist看起来像JSON,而不是XML

我可以通过编辑~/MacOSX/environment让Spotlight应用程序看到变量。plist和 我能够通过在我的.profile文件中添加以下内容将相同的vars强制到一个新的Terminal会话中:

eval $(launchctl export)

其他回答

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

touch .bash_profile

then

open -a TextEdit.app .bash_profile

add

export TOMCAT_HOME=/Library/Tomcat/Home

保存文档,您就完成了。

布鲁诺正在正确的轨道上。我已经做了大量的研究,如果你想设置在所有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.

有时候,前面所有的答案都不适用。如果你想在Eclipse或IntelliJ IDEA中访问一个系统变量(如M2_HOME),在这种情况下,唯一适合我的是:

首先(步骤1)编辑/etc/launchd.conf以包含如下一行:“setenv VAR value”,然后(步骤2)重新启动。

简单地修改.bash_profile将不起作用,因为在OS X中应用程序不会像在其他Unix中那样启动;它们不继承父函数的壳变量。其他的修改都不管用了,原因我也不知道。也许有人可以解释一下。

iOS上的所有神奇之处都是使用source和文件,在这里导出环境变量。

例如:

你可以像这样创建一个文件:

export bim=fooo
export bom=bar

保存为bimbom。Env,然后做source ./bimbom.ev。 Voilá,你有你的环境变量。

检查它们:

echo $bim

另一个免费、开源的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”)。