macos 在 Mac OS X Lion 上设置环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7501678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:27:28  来源:igfitidea点击:

Set environment variables on Mac OS X Lion

macosenvironment-variablesosx-lion

提问by Anders

When someone says "edit your .plist file" or "your .profile" or ".bash_profile" etc, this just confuses me. I have no idea where these files are, how to create them if I have to do that, etc, and also why there seem to be so many different ones (why? Do they do different things?)

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

So could someone please explain very patiently to a previous Windows user (wanting desperately to become more familiar with the pleasant if initially somewhat confusing OS X world) how to do this step by step?

那么有人可以非常耐心地向以前的 Windows 用户解释如何一步一步地做到这一点吗?

I need the variables to be set both for GUI applications and command line applications, and at the moment it's for an ant script that needs the variables, but there will most likely be other needs as well.

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

Please note that I have Lion too, since many of the answers you get Googling seem to be outdated for Lion...

请注意,我也有 Lion,因为您在谷歌上搜索的许多答案对于 Lion 来说似乎已经过时了……

Also note that I have practically zero experience using the Terminal. I'm willing to learn, but please explain for a novice...

另请注意,我使用终端的经验几乎为零。我愿意学习,但请为新手解释...

采纳答案by David V

First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:

首先,要了解 OS X 的一件事是它构建在 Unix 上。这是 .bash_profile 的用武之地。当您在 OS X 中启动终端应用程序时,默认情况下您会获得一个 bash shell。bash shell 来自 Unix,当它加载时,它运行 .bash_profile 脚本。您可以为您的用户修改此脚本以更改您的设置。该文件位于:

~/.bash_profile


Update for Mavericks

小牛队更新

OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configurationfor windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.

OS X Mavericks 不使用 environment.plist - 至少不用于 OS X Windows 应用程序。您可以将launchd 配置用于窗口化应用程序。.bash_profile 仍然受支持,因为它是终端中使用的 bash shell 的一部分。

Lion and Mountain Lion Only

仅限狮子和山狮

OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:

OS X 窗口化应用程序从您的 environment.plist 文件接收环境变量。这可能就是您所说的“.plist”文件的意思。该文件位于:

~/.MacOSX/environment.plist


If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.

如果您对 environment.plist 文件进行更改,则 OS X Windows 应用程序(包括终端应用程序)将设置这些环境变量。您在 .bash_profile 中设置的任何环境变量只会影响您的 bash shell。

Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).

通常我只在我的 .bash_profile 文件中设置变量并且不更改 .plist 文件(或小牛上的 launchd 文件)。大多数 OS X 窗口化应用程序不需要任何自定义环境。只有当应用程序实际上需要特定的环境变量时,我才更改 environment.plist(或小牛上的启动文件)。

It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.

听起来您想要的是更改 environment.plist 文件,而不是 .bash_profile。

One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.

最后一件事,如果您查找这些文件,我想您不会找到它们。如果我没记错的话,它们不在我最初安装的 Lion 中。

Edit:Here are some instructions for creating a plist file.

编辑:以下是创建 plist 文件的一些说明。

  1. Open Xcode
  2. Select File -> New -> New File...
  3. Under Mac OS X select Resources
  4. Choose a plist file
  5. Follow the rest of the prompts
  1. 打开 Xcode
  2. 选择文件 -> 新建 -> 新建文件...
  3. 在 Mac OS X 下选择资源
  4. 选择一个 plist 文件
  5. 按照其余提示操作

To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.

要编辑文件,您可以按住 Control 单击以获取菜单并选择添加行。然后您可以添加一个键值对。对于环境变量,键是环境变量名称,值是该环境变量的实际值。

Once the plist file is created you can open it with Xcode to modify it anytime you wish.

创建 plist 文件后,您可以随时使用 Xcode 打开它以进行修改。

回答by tokkov

Your .profileor .bash_profileare 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 lsto list files you still won't see them. However, you can find them by using ls -ain 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.bash_profile只是存在于“home”文件夹中的文件。如果您打开 Finder 窗口并在“收藏夹”面板中单击您的帐户名称,您将看不到它们。如果您打开终端窗口并键入ls以列出文件,您仍然看不到它们。但是,您可以通过ls -a在终端中使用来找到它们。或者,如果您打开您最喜欢的文本编辑器(比如 OS X 附带的 TextEdit)并执行 File->Open,然后按Command+ Shift+.并单击您的帐户名(主文件夹),您也会看到它们。如果您没有看到它们,那么您可以在您喜欢的文本编辑器中创建一个。

Now, adding environment variables is relatively straightforward and remarkably similar to windows conceptually. In your .profilejust add, one per line, the variable name and its value as follows:

现在,添加环境变量相对简单,并且在概念上与 windows 非常相似。在您.profile刚刚添加,每行一个,变量名称及其值如下:

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

etc.

等等。

If you are modifying your "PATH" variable, be sure to include the system's default PATH that was already set for you:

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

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

Now here is the quirky part, you can either open a new Terminal window to have the new variables take effect, or you will need to type .profileor .bash_profileto reload the file and have the contents be applied to your current Terminal's environment.

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

You can check that your changes took effect using the "set" command in your Terminal. Just type set(or set | moreif you prefer a paginated list) and be sure what you added to the file is there.

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

As for adding environment variables to GUI apps, that is normally not necessary and I'd like to hear more about what you are specifically trying to do to better give you an answer for it.

至于将环境变量添加到 GUI 应用程序,这通常是没有必要的,我想了解更多有关您特别想做什么以更好地为您提供答案的信息。

回答by jonphipps

Here's a bit more information specifically regarding the PATH variable in Lion OS 10.7.x:

以下是有关 Lion OS 10.7.x 中 PATH 变量的更多信息:

If you need to set the PATH globally, the PATH is built by the system in the following order:

如果需要全局设置PATH,系统会按照以下顺序构建PATH:

  1. Parsing the contents of the file /private/etc/paths, one path per line
  2. 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.
  3. A setenv PATHstatement in /private/etc/launchd.conf, which will appendthat 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.
  1. 解析文件内容,/private/etc/paths每行一个路径
  2. 解析文件夹的内容/private/etc/paths.d。该文件夹中的每个文件可以包含多个路径,每行一个路径。加载顺序首先由文件名决定,然后是文件中行的顺序。
  3. 中的一条setenv PATH语句/private/etc/launchd.conf,它将将该路径附加到已在 #1 和 #2 中构建的路径(您不得使用 $PATH 来引用迄今为止已构建的 PATH 变量)。但是,考虑到其他两个选项,在这里设置 PATH 完全没有必要,尽管这是可以为所有用户设置其他全局环境变量的地方。

These paths and variables are inherited by all users and applications, so they are truly global -- logging out and in will not reset these paths -- they're built for the system and are created before any user is given the opportunity to login, so changes to these require a system restart to take effect.

这些路径和变量由所有用户和应用程序继承,因此它们是真正全局的——注销和登录不会重置这些路径——它们是为系统构建的,并且在任何用户有机会登录之前创建,因此对这些的更改需要重新启动系统才能生效。

BTW, a clean install of OS 10.7.x Lion doesn't have an environment.plistthat I can find, so it may work but may also be deprecated.

顺便说一句,全新安装的 OS 10.7.x Lion 没有environment.plist我能找到的,所以它可能工作但也可能被弃用。

回答by learner_19

echo $PATH

it prints current path value

它打印当前路径值

Then do vim ~/.bash_profileand write

然后做vim ~/.bash_profile和写

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

here you are appending to the old path, so preserves the old path and adds your new path to it

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

then do

然后做

source ~/.bash_profile

this will execute it and add the path

这将执行它并添加路径

then again check with

然后再次检查

echo $PATH

回答by wired00

Unfortunately none of these answers solved the specific problem I had.

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

Here's a simple solution without having to mess with bash. In my case, it was getting gradleto work (for Android Studio).

这是一个简单的解决方案,而不必弄乱bash. 就我而言,它开始gradle工作了(对于Android Studio)。

Btw, These steps relate to OSX (Mountain Lion 10.8.5)

顺便说一句,这些步骤与 OSX (Mountain Lion 10.8.5) 有关

  • Open up Terminal.
  • Run the following command:

    sudo nano /etc/paths(or sudo vim /etc/pathsfor vim)

    nano

  • Go to the bottom of the file, and enter the path you wish to add.
  • Hit control-x to quit.
  • Enter 'Y' to save the modified buffer.
  • Open a new terminal window then type:

    echo $PATH

  • 打开终端。
  • 运行以下命令:

    sudo nano /etc/paths(或sudo vim /etc/paths对于 vim)

    纳米

  • 转到文件底部,然后输入要添加的路径。
  • 按 control-x 退出。
  • 输入“Y”以保存修改后的缓冲区。
  • 打开一个新的终端窗口,然后输入:

    echo $PATH

You should see the new path appended to the end of the PATH

您应该会看到附加到 PATH 末尾的新路径

I got these details from this post:

我从这篇文章中得到了这些细节:

http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q

http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q

I hope that can help someone else

我希望能帮助别人

回答by vol7ron

Simplified Explanation

简化说明

This post/question is kind of old, so I will answer a simplified version for OS X Lion users. By default, OSX Lion does not have any of the following files:

这篇文章/问题有点陈旧,所以我将为 OS X Lion 用户回答一个简化版本。默认情况下,OSX Lion 没有以下任何文件:

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

At most, if you've done anything in the terminal you might see ~/.bash_history

至多,如果您在终端中做过任何事情,您可能会看到 ~/.bash_history

What It Means

这是什么意思

You must create the file to set your default bash commands (commonly in ~/.bashrc). To do this, use any sort of editor, though it's more simple to do it within the terminal:

您必须创建文件来设置默认的 bash 命令(通常在 ~/.bashrc 中)。为此,请使用任何类型的编辑器,但在终端中进行更简单:

  1. %> emacs .profile
  2. [from w/in emacs type:] source ~/.bashrc
  3. [from w/in emacs type:] Ctrl + x Ctrl + s(to save the file)
  4. [from w/in emacs type:] Ctrl + x Ctrl + c(to close emacs)
  5. %> emacs .bashrc
  6. [from w/in emacs type/paste all your bash commands, save, and exit]
  1. %> emacs .profile
  2. [来自 emacs 类型:] source ~/.bashrc
  3. [来自 w/in emacs 类型:] Ctrl + x Ctrl + s(保存文件)
  4. [来自 w/in emacs 类型:] Ctrl + x Ctrl + c(关闭 emacs)
  5. %> emacs .bashrc
  6. [在 emacs 中输入/粘贴所有 bash 命令,保存并退出]

The next time you quit and reload the terminal, it should load all your bash preferences. For good measure, it's usually a good idea to separate your commands into useful file names. For instance, from within ~/.bashrc, you should have a source ~/.bash_aliasesand put all your alias commands in ~/.bash_aliases.

下次退出并重新加载终端时,它应该加载所有 bash 首选项。为了更好地衡量,将命令分成有用的文件名通常是个好主意。例如,从 ~/.bashrc 中,您应该有一个source ~/.bash_aliases并将所有别名命令放在 ~/.bash_aliases 中。

回答by Gilimanjaro

What worked for me is to create a .launchd.confwith the variables I needed:

对我有用的是.launchd.conf用我需要的变量创建一个:

setenv FOO barbaz

This file is read by launchd at login. You can add a variable 'on the fly' to the running launchd using:

此文件在登录时由 launchd 读取。您可以使用以下命令将变量“即时”添加到正在运行的 launchd 中:

launchctl setenv FOO barbaz`

In fact, .launchd.condsimply contains launchctl commands.

事实上,.launchd.cond只包含launchctl命令。

Variables set this way seem to be present in GUI applications properly.

以这种方式设置的变量似乎正确存在于 GUI 应用程序中。

If you happen to be trying to set your LANG or LC_ variables in this way, and you happen to be using iTerm2, make sure you disable the 'Set locale variables automatically' setting under the Terminal tab of the Profile you're using. That seems to override launchd's environment variables, and in my case was setting a broken LC_CTYPE causing issues on remote servers (which got passed the variable).

如果您碰巧尝试以这种方式设置 LANG 或 LC_ 变量,并且碰巧使用 iTerm2,请确保禁用您正在使用的配置文件的终端选项卡下的“自动设置区域设置变量”设置。这似乎覆盖了 launchd 的环境变量,在我的情况下是设置损坏的 LC_CTYPE 导致远程服务器出现问题(通过变量)。

(The environment.plist still seems to work on my Lion though. You can use the RCenvironmentpreference pane to maintain the file instead of manually editing it or required Xcode. Still seems to work on Lion, though it's last update is from the Snow Leopard era. Makes it my personally preferred method.)

(虽然 environment.plist 似乎仍然适用于我的 Lion。您可以使用RCenvironment首选项窗格来维护文件,而不是手动编辑它或需要 Xcode。似乎仍然适用于 Lion,尽管它的最后一次更新来自 Snow Leopard时代。使它成为我个人首选的方法。)

回答by Charaf JRA

Setup your PATH environment variable on Mac OS

在 Mac OS 上设置 PATH 环境变量

Open the Terminal program (this is in your Applications/Utilites folder by default). Run the following command

打开终端程序(默认情况下位于您的应用程序/实用程序文件夹中)。运行以下命令

touch ~/.bash_profile; open ~/.bash_profile

This will open the file in the your default text editor.

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

For ANDROID SDK as example :

以 ANDROID SDK 为例:

You need to add the path to your Android SDK platform-tools and tools directory. In my example I will use "/Development/android-sdk-macosx" as the directory the SDK is installed in. Add the following line:

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

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

Save the file and quit the text editor. Execute your .bash_profile to update your PATH.

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

source ~/.bash_profile

Now everytime you open the Terminal program you PATH will included the Android SDK.

现在,每次打开终端程序时,PATH 都会包含 Android SDK。

回答by Yaw Joseph Etse

Adding Path Variables to OS X Lion

将路径变量添加到 OS X Lion

This was pretty straight forward and worked for me, in terminal:

这非常简单,在终端对我有用:

$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

A similar response was here: http://www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516

类似的回应在这里:http: //www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516

回答by Do Nhu Vy

Open Terminal:

打开终端:

vi ~/.bash_profile

Apply changing to system (no need restart computer):

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

source ~/.bash_profile

(Also work with macOS Sierra 10.12.1)

(也适用于 macOS Sierra 10.12.1)