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

其他回答

添加路径变量到OS X Lion

这是非常直接的,对我来说很有用,在终端:

$echo "export PATH=$PATH:/path/to/whatever" >> .bash_profile #replace "/path/to/whatever" with the location of what you want to add to your bash profile, i.e: $ echo "export PATH=$PATH:/usr/local/Cellar/nginx/1.0.12/sbin" >> .bash_profile 
$. .bash_profile #restart your bash shell

类似的回复在这里:http://www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516

首先,要认识到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打开它,随时修改它。

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

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.

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

这里有一个简单的解决方案,不需要干扰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

我希望这能帮助到其他人

步骤1:打开~/.bash_profile

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

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

步骤3:导出NODE_ENV=development

保存并关闭。

重新启动系统。

完成了。

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

echo $NODE_ENV