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

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

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

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

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


当前回答

步骤1:打开~/.bash_profile

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

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

步骤3:导出NODE_ENV=development

保存并关闭。

重新启动系统。

完成了。

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

echo $NODE_ENV 

其他回答

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

Your .profile or .bash_profile are simply files that are present in your "home" folder. If you open a Finder window and click your account name in the Favorites pane, you won't see them. If you open a Terminal window and type ls to list files you still won't see them. However, you can find them by using ls -a in the terminal. Or if you open your favorite text editor (say TextEdit since it comes with OS X) and do File->Open and then press Command+Shift+. and click on your account name (home folder) you will see them as well. If you do not see them, then you can create one in your favorite text editor.

现在,添加环境变量相对简单,在概念上与窗口非常相似。在你的.profile中,每行添加一个变量名和它的值,如下所示:

export JAVA_HOME=/Library/Java/Home
export JRE_HOME=/Library/Java/Home

etc.

如果你正在修改你的"PATH"变量,请确保包含系统已经为你设置的默认PATH:

export PATH=$PATH:/path/to/my/stuff

现在是奇怪的部分,您可以打开一个新的终端窗口以使新变量生效,或者您需要键入.profile或.bash_profile来重新加载文件,并将内容应用到当前终端的环境中。

您可以在终端中使用“set”命令检查更改是否生效。只需输入set(如果您喜欢分页列表,可以再设置|),并确保您添加到文件中的内容。

至于在GUI应用程序中添加环境变量,这通常是不必要的,我想听到更多关于你正在具体尝试做什么,以便更好地给你一个答案。

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

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.

打开终端:

vi ~/.bash_profile

将更改应用于系统(无需重启计算机):

source ~/.bash_profile

(也适用于macOS Sierra 10.12.1)

建议在设置任何环境变量之前检查默认的终端shell,通过以下命令:

$ echo $SHELL
/bin/zsh

如果您的默认终端是/bin/zsh (Z Shell),就像我的情况(个人更喜欢Z Shell)一样,那么您应该在~/中设置这些环境变量。zshenv文件,内容如下(本例中设置JAVA_HOME环境变量,其他情况相同):

export JAVA_HOME="$(/usr/libexec/java_home)"

类似地,上面没有提到的任何其他终端类型,都应该在其各自的终端env文件中设置环境变量。