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

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

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

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

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


当前回答

我在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

其他回答

我在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

不幸的是,这些答案都没有解决我遇到的具体问题。

这里有一个简单的解决方案,不需要干扰bash。在我的例子中,它是让gradle工作(Android Studio)。

顺便说一句,这些步骤涉及OSX (Mountain Lion 10.8.5)

打开终端。 执行如下命令: Sudo nano /etc/paths(或Sudo vim /etc/paths for vim) 转到文件底部,输入要添加的路径。 按control-x退出。 输入“Y”保存修改后的缓冲区。 打开一个新的终端窗口,然后输入: 回声路径美元

您应该看到新路径被追加到path的末尾

我从这篇文章中得到了这些细节:

http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q

我希望这能帮助到其他人

更多细节,这可能会对某人有所帮助:

Due to my own explorations, I now know how to set environment variables in 7 of 8 different ways. I was trying to get an envar through to an application I'm developing under Xcode. I set "tracer" envars using these different methods to tell me which ones get it into the scope of my application. From the below, you can see that editing the "scheme" in Xcode to add arguments works, as does "putenv". What didn't set it in that scope: ~/.MACOS/environment.plist, app-specific plist, .profile, and adding a build phase to run a custom script (I found another way in Xcode [at least] to set one but forgot what I called the tracer and can't find it now; maybe it's on another machine....)

GPU_DUMP_DEVICE_KERNEL为3

GPU_DUMP_TRK_ENVPLIST是(null)

GPU_DUMP_TRK_APPPLIST是(null)

GPU_DUMP_TRK_DOTPROFILE是(null)

GPU_DUMP_TRK_RUNSCRIPT是(null)

GPU_DUMP_TRK_SCHARGS为1

GPU_DUMP_TRK_PUTENV为1

... 另一方面,如果我进入终端并说“设置”,它似乎唯一得到的是一个来自.profile(我本以为它会捡到环境。plist也是,而且我确信我曾经在终端中看到第二个跟踪程序envar,所以从那以后可能有什么东西不稳定了。漫长的一天……)

以下是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,所以它可能工作,但也可能被弃用。

我选择了白痴路线。 将这些添加到/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