在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)。
当前回答
登录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文件中是一个好主意。
其他回答
虽然这里的答案并不是“错误的”,但我还要补充一点:永远不要在OS X中改变影响“所有进程”的环境变量,甚至在shell之外,也不要影响给定用户交互运行的所有进程。
In my experience, global changes to environment variables like PATH for all processes are even more likely to break things on OS X than on Windows. Reason being, lots of OS X applications and other software (including, perhaps especially, components of the OS itself) rely on UNIX command-line tools under the hood, and assume the behavior of the versions of these tools provided with the system, and don't necessarily use absolute paths when doing so (similar comments apply to dynamically-loaded libraries and DYLD_* environment variables). Consider, for instance, that the highest-rated answers to various Stack Overflow questions about replacing OS X-supplied versions of interpreters like Python and Ruby generally say "don't do this."
OS X is really no different than other UNIX-like operating systems (e.g., Linux, FreeBSD, and Solaris) in this respect; the most likely reason Apple doesn't provide an easy way to do this is because it breaks things. To the extent Windows isn't as prone to these problems, it's due to two things: (1) Windows software doesn't tend to rely on command-line tools to the extent that UNIX software does, and (2) Microsoft has had such an extensive history of both "DLL hell" and security problems caused by changes that affect all processes that they've changed the behavior of dynamic loading in newer Windows versions to limit the impact of "global" configuration options like PATH.
不管“蹩脚”与否,如果您将这些更改限制在较小的范围内,您将拥有一个更加稳定的系统。
布鲁诺正在正确的轨道上。我已经做了大量的研究,如果你想设置在所有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.
这里有两种类型的炮弹。
非登录:每次启动一个新的Bash副本时,.bashrc都会被重新加载 登录:.profile只有在登录或显式告诉Bash加载它并将其用作登录shell时才会被加载。
在这里,重要的是要理解在Bash中,.bashrc文件只能由交互式和非登录的shell读取,您会发现人们经常在.bash_profile中加载.bashrc以克服这一限制。
现在您已经有了基本的了解,让我们继续讨论我建议您如何设置它。
.profile:创建一个不存在的文件。把你的PATH设置放在那里。 .bashrc:如果不存在则创建。把你所有的别名和自定义方法都放进去。 .bash_profile:如果不存在则创建。把下面的东西放进去。
.bash_file:
#!/bin/bash
source ~/.profile # Get the PATH settings
source ~/.bashrc # Get Aliases and Functions
#
这很简单。编辑文件.profile (vi, nano, Sublime Text或其他文本编辑器)文件。你可以在~/目录(用户目录)中找到它,并像这样设置:
export MY_VAR=[your value here]
Java home的示例:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/current
保存它并返回到终端。
你可以用:
source .profile
或者关闭并打开终端窗口。
登录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文件中是一个好主意。