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

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

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

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

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


当前回答

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

其他回答

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

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.

更多细节,这可能会对某人有所帮助:

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,所以从那以后可能有什么东西不稳定了。漫长的一天……)

添加路径变量到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 Lion用户。 默认情况下,OSX Lion没有以下任何文件:

~ / . bashrc ~ / . bash_profile ~ / . profile

最多,如果您在终端中执行了任何操作,您可能会看到~/.bash_history

它的含义

您必须创建这个文件来设置默认的bash命令(通常在~/.bashrc中)。要做到这一点,可以使用任何类型的编辑器,尽管在终端中更简单:

%> emacs .profile [from w/in emacs type:] source ~/.bashrc . [from w/in emacs type:] Ctrl + x Ctrl + s(保存文件) [from w/in emacs type:] Ctrl + x Ctrl + c(关闭emacs) %> emacs .bashrc [从w/in emacs输入/粘贴所有bash命令,保存并退出]

下次退出并重新加载终端时,它应该加载所有bash首选项。另外,将命令分割成有用的文件名通常是个好主意。例如,from within ~/.Bashrc,你应该有一个源~/。Bash_aliases并将所有别名命令放在~/. Bash_aliases中。

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