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

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

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

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

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


当前回答

在Mac OS上设置PATH环境变量

打开终端程序(默认在应用程序/实用程序文件夹中)。 运行以下命令

touch ~/.bash_profile; open ~/.bash_profile

这将在默认文本编辑器中打开文件。

ANDROID SDK为例:

你需要添加路径到你的Android SDK平台工具和工具目录。在我的例子中,我将使用“/Development/android-sdk-macosx”作为SDK的安装目录。增加如下一行:

export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools

保存文件并退出文本编辑器。 执行.bash_profile来更新PATH。

source ~/.bash_profile

现在,每次你打开终端程序,你的PATH将包括Android SDK。

其他回答

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

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)

添加路径变量到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

对我来说有用的是用我需要的变量创建一个.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。狮子似乎仍然工作,虽然它的最后一次更新是从雪豹时代。这是我个人最喜欢的方法。)

在Mac OS上设置PATH环境变量

打开终端程序(默认在应用程序/实用程序文件夹中)。 运行以下命令

touch ~/.bash_profile; open ~/.bash_profile

这将在默认文本编辑器中打开文件。

ANDROID SDK为例:

你需要添加路径到你的Android SDK平台工具和工具目录。在我的例子中,我将使用“/Development/android-sdk-macosx”作为SDK的安装目录。增加如下一行:

export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools

保存文件并退出文本编辑器。 执行.bash_profile来更新PATH。

source ~/.bash_profile

现在,每次你打开终端程序,你的PATH将包括Android SDK。