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

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

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

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

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


当前回答

步骤1:打开~/.bash_profile

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

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

步骤3:导出NODE_ENV=development

保存并关闭。

重新启动系统。

完成了。

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

echo $NODE_ENV 

其他回答

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

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

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

以下是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