当有人说“编辑你的。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

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

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,所以从那以后可能有什么东西不稳定了。漫长的一天……)

echo $PATH

它输出当前路径值

然后执行vim ~/。Bash_profile并写入

export PATH=$PATH:/new/path/to/be/added

在这里,您将追加到旧路径,因此保留旧路径并将新路径添加到其中

然后做

source ~/.bash_profile

这将执行它并添加路径

然后再次检查

echo $PATH

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

步骤1:打开~/.bash_profile

现在打开一个文本编辑器:

Step2:变量名应为大写。本例中的变量为NODE_ENV

步骤3:导出NODE_ENV=development

保存并关闭。

重新启动系统。

完成了。

查看env变量:打开终端并输入

echo $NODE_ENV