当有人说“编辑你的。plist文件”或“你的。profile”或“。Bash_profile”等等,这让我很困惑。我不知道这些文件在哪里,如果我必须这样做,如何创建它们,等等,以及为什么似乎有这么多不同的(为什么?)他们做不同的事情吗?)

所以,谁能非常耐心地向以前的Windows用户(迫切地想要更加熟悉最初有点困惑的OS X世界)解释如何一步一步地做到这一点?

我需要为GUI应用程序和命令行应用程序设置变量,目前是为需要变量的ant脚本设置变量,但很可能还有其他需求。

请注意,我也有Lion,因为你在谷歌上得到的许多答案似乎已经过时了…

还要注意,我几乎没有使用终端的经验。我愿意学习,但请为一个新手解释一下……


当前回答

让我用我个人的例子,用一种非常多余的方式来说明。

First after installing JDK, make sure it's installed. Sometimes macOS or Linux automatically sets up environment variable for you unlike Windows. But that's not the case always. So let's check it. The line immediately after echo $JAVA_HOME would be empty if the environment variable is not set. It must be empty in your case. Now we need to check if we have bash_profile file. You saw that in my case we already have bash_profile. If not we have to create a bash_profile file. Create a bash_profile file. Check again to make sure bash_profile file is there. Now let's open bash_profile file. macOS opens it using it's default TextEdit program. This is the file where environment variables are kept. If you have opened a new bash_profile file, it must be empty. In my case, it was already set for python programming language and Anaconda distribution. Now, i need to add environment variable for Java which is just adding the first line. YOU MUST TYPE the first line VERBATIM. JUST the first line. Save and close the TextEdit. Then close the terminal. Open the terminal again. Let's check if the environment variable is set up.

其他回答

以下是Lion OS 10.7.x中关于PATH变量的更多信息:

如果需要全局设置PATH,则PATH由系统按照如下顺序构建:

Parsing the contents of the file /private/etc/paths, one path per line Parsing the contents of the folder /private/etc/paths.d. Each file in that folder can contain multiple paths, one path per line. Load order is determined by the file name first, and then the order of the lines in the file. A setenv PATH statement in /private/etc/launchd.conf, which will append that path to the path already built in #1 and #2 (you must not use $PATH to reference the PATH variable that has been built so far). But, setting the PATH here is completely unnecessary given the other two options, although this is the place where other global environment variables can be set for all users.

这些路径和变量由所有用户和应用程序继承,因此它们是真正的全局路径——登出和登录不会重置这些路径——它们是为系统构建的,是在任何用户有机会登录之前创建的,因此对这些路径的更改需要重新启动系统才能生效。

顺便说一句,OS 10.7的干净安装。狮子没有环境。我可以找到plist,所以它可能工作,但也可能被弃用。

首先,要认识到OS X是建立在Unix之上的。这就是.bash_profile的用武之地。当你在OS X中启动Terminal应用程序时,默认情况下你会得到一个bash shell。bash shell来自Unix,当它加载时,它会运行.bash_profile脚本。您可以为您的用户修改此脚本以更改您的设置。这个文件位于:

~/.bash_profile

小牛队更新

OS X Mavericks不使用环境。plist -至少不是OS X windows应用程序。您可以为窗口应用程序使用launchd配置。仍然支持.bash_profile,因为它是终端中使用的bash shell的一部分。

只限狮子和山狮

OS X窗口应用程序从您的环境接收环境变量。plist文件。这可能就是你所说的“。”plist文件”。这个文件位于:

~/.MacOSX/environment.plist

如果你改变你的环境。plist文件,那么OS X windows应用程序,包括终端应用程序,将设置这些环境变量。您在.bash_profile中设置的任何环境变量只会影响您的bash shell。

通常我只在.bash_profile文件中设置变量,而不更改.plist文件(或Mavericks上的launchd文件)。大多数OS X窗口应用程序不需要任何自定义环境。只有当应用程序实际需要特定的环境变量时,我才更改环境。plist(或Mavericks上的launchd文件)。

听起来你想要的是改变环境。Plist文件,而不是.bash_profile。

最后一件事,如果你找那些文件,我想你是找不到的。如果我没记错的话,在我最初安装Lion时并没有。

编辑:这里有一些创建plist文件的说明。

打开Xcode Select File -> New -> New File… 在Mac OS X下选择“资源” 选择一个plist文件 按照剩下的提示操作

要编辑该文件,可以通过Control-click获得菜单并选择Add Row。然后可以添加键值对。对于环境变量,键是环境变量名,值是该环境变量的实际值。

一旦创建了plist文件,你可以用Xcode打开它,随时修改它。

对我来说有用的是用我需要的变量创建一个.launchd.conf:

setenv FOO barbaz

这个文件在登录时由launchd读取。你可以添加一个变量'on the fly'到运行的launchd,使用:

launchctl setenv FOO barbaz`

事实上,。launchd。Cond只包含launchctl命令。

以这种方式设置的变量似乎正确地呈现在GUI应用程序中。

如果你碰巧试图以这种方式设置LANG或LC_变量,并且你碰巧使用的是iTerm2,请确保你禁用了你正在使用的概要文件的终端选项卡下的“自动设置区域变量”设置。这似乎覆盖了launchd的环境变量,在我的情况下,设置了一个坏的LC_CTYPE导致远程服务器上的问题(这被传递了变量)。

(环境。plist似乎仍然工作在我的狮子虽然。您可以使用RCenvironment首选项窗格来维护文件,而不是手动编辑它或所需的Xcode。狮子似乎仍然工作,虽然它的最后一次更新是从雪豹时代。这是我个人最喜欢的方法。)

我在Maverics上使用Eclipse(从GUI开始,而不是从脚本开始)时遇到了问题,它没有采用自定义PATH。我尝试了上面提到的所有方法,但都无济于事。最后,我根据这里的提示找到了最简单的答案:

Go to /Applications/eclipse/Eclipse.app/Contents folder Edit Info.plist file with text editor (or XCode), add LSEnvironment dictionary for environment variable with full path. Note that it includes also /usr/bin etc: <dict> <key>LSEnvironment</key> <dict> <key>PATH</key> <string>/usr/bin:/bin:/usr/sbin:/sbin:/dev/android-ndk-r9b</string> </dict> <key>CFBundleDisplayName</key> <string>Eclipse</string> ... Reload parameters for app with /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.fra‌​mework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app Restart Eclipse

我选择了白痴路线。 将这些添加到/etc/profile的末尾

for environment in `find /etc/environments.d -type f`
do
     . $environment
done

创建文件夹/etc/environments 在其中创建一个名为“oracle”或“whatever”的文件,并添加我需要全局设置的东西。

/etc$ cat /etc/environments.d/Oracle

export PATH=$PATH:/Library/Oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=/Library/Oracle/instantclient_11_2
export SQLPATH=/Library/Oracle/instantclient_11_2
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export TNS_ADMIN=/Library/Oracle/instantclient_11_2/network/admin