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

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

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

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

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


当前回答

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

这里有一个简单的解决方案,不需要干扰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,所以从那以后可能有什么东西不稳定了。漫长的一天……)

echo $PATH

它输出当前路径值

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

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

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

然后做

source ~/.bash_profile

这将执行它并添加路径

然后再次检查

echo $PATH

步骤1:打开~/.bash_profile

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

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

步骤3:导出NODE_ENV=development

保存并关闭。

重新启动系统。

完成了。

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

echo $NODE_ENV 

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

$ echo $SHELL
/bin/zsh

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

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

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

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