Mac OS X中的环境变量。

时间:2021-09-07 11:03:24

Update: The link below does not have a complete answer. Having to set the path or variable in two places (one for GUI and one for shell) is lame.

更新:下面的链接没有完整的答案。必须在两个地方设置路径或变量(一个用于GUI,另一个用于shell)是很蹩脚的。

Not Duplicate of: Setting environment variables in OS X?

不重复:在OS X中设置环境变量?


Coming from a Windows background where it's very easy to set and modify environment variables (just go to System Properties > Advanced > Environment Variables), it does not seem to be that straight forward on Mac OS 10.5. Most references say I should update /etc/profile or ~/.profile. Are those the equivalent of System Variables and User Variables? For example, where should I set my JAVA_HOME variable?

从Windows的背景中,可以很容易地设置和修改环境变量(只需要访问系统属性>高级>环境变量),它在Mac OS 10.5上似乎并不是那么简单。大多数引用都说我应该更新/etc/profile或~/.profile。这些是等价的系统变量和用户变量吗?例如,我应该在哪里设置JAVA_HOME变量?


EDIT:

编辑:

I want to be able to access the variable from the terminal as well as an app like Eclipse. Also, I hope I don't have to restart/logout to make this take effect.

我希望能够从终端访问变量以及像Eclipse这样的应用程序。另外,我希望我不用重新启动/退出来让它生效。

8 个解决方案

#1


132  

There's no need for duplication. You can set environment variables used by launchd (and child processes, i.e. anything you start from Spotlight) using launchctl setenv.

没有必要重复。您可以使用launchctl setenv来设置launchd(和子进程,即您从聚光灯开始的任何东西)所使用的环境变量。

For example, if you want to mirror your current path in launchd after setting it up in .bashrc or wherever:

例如,如果您想要在设置为.bashrc或其他任何地方之后,在launchd中镜像您当前的路径:

PATH=whatever:you:want
launchctl setenv PATH $PATH

Environment variables are not automatically updated in running applications. You will need to relaunch applications to get the updated environment variables (although you can just set variables in your shell, e.g. PATH=whatever:you:want; there's no need to relaunch the terminal).

在运行的应用程序中,环境变量不会自动更新。您需要重新启动应用程序以获得更新的环境变量(尽管您可以在shell中设置变量,例如PATH=您:想要的);没有必要重新启动终端。

#2


272  

There are several places where you can set environment variables.

有几个地方可以设置环境变量。

  • ~/.profile: use this for variables you want to set in all programs launched from the terminal (note that, unlike on Linux, all shells opened in Terminal.app are login shells).
  • ~ /。配置文件:将此用于您想要从终端启动的所有程序中设置的变量(注意,与Linux不同的是,所有shell都在终端中打开。应用程序登录shell)。
  • ~/.bashrc: this is invoked for shells which are not login shells. Use this for aliases and other things which need to be redefined in subshells, not for environment variables that are inherited.
  • ~ /。bashrc:这是调用没有登录shell的shell。用于别名和其他需要在子shell中重新定义的东西,而不是用于继承的环境变量。
  • /etc/profile: this is loaded before ~/.profile, but is otherwise equivalent. Use it when you want the variable to apply to terminal programs launched by all users on the machine (assuming they use bash).
  • /etc/profile:这是在~/之前加载的。配置文件,但在其他方面是等同的。当您希望变量应用于机器上所有用户启动的终端程序时(假设它们使用bash),请使用它。
  • ~/.MacOSX/environment.plist: this is read by loginwindow on login. It applies to all applications, including GUI ones, except those launched by Spotlight in 10.5 (not 10.6). It requires you to logout and login again for changes to take effect. This file is no longer supported as of OS X 10.8.
  • ~ / .MacOSX /环境。plist:登录时通过loginwindow读取。它适用于所有的应用程序,包括GUI的应用程序,除了那些在10.5(不是10.6)中发布的应用程序。它要求您注销并重新登录,以便更改生效。这个文件不再支持OS X 10.8。
  • your user's launchd instance: this applies to all programs launched by the user, GUI and CLI. You can apply changes at any time by using the setenv command in launchctl. In theory, you should be able to put setenv commands in ~/.launchd.conf, and launchd would read them automatically when the user logs in, but in practice support for this file was never implemented. Instead, you can use another mechanism to execute a script at login, and have that script call launchctl to set up the launchd environment.
  • 您的用户的launchd实例:这适用于由用户、GUI和CLI发起的所有程序。您可以在任何时候使用launchctl中的setenv命令来应用更改。理论上,您应该能够将setenv命令放在~/.launchd中。conf和launchd会在用户登录时自动读取它们,但是在实践中对该文件的支持从未实现。相反,您可以使用另一种机制来在登录时执行脚本,并使用该脚本调用launchctl来设置launchd环境。
  • /etc/launchd.conf: this is read by launchd when the system starts up and when a user logs in. They affect every single process on the system, because launchd is the root process. To apply changes to the running root launchd you can pipe the commands into sudo launchctl.
  • /etc/launchd.conf:当系统启动时,当用户登录时,就可以读取。它们影响系统中的每一个进程,因为launchd是根进程。要将更改应用到运行的root launchd,您可以将命令输入sudo launchctl。

The fundamental things to understand are:

最基本的理解是:

  • environment variables are inherited by a process's children at the time they are forked.
  • 环境变量是由进程的子节点在它们被分叉时继承的。
  • the root process is a launchd instance, and there is also a separate launchd instance per user session.
  • 根进程是一个launchd实例,并且每个用户会话也有一个单独的launchd实例。
  • launchd allows you to change its current environment variables using launchctl; the updated variables are then inherited by all new processes it forks from then on.
  • launchd允许您使用launchctl更改其当前环境变量;然后,更新后的变量将由它从那时开始的所有新进程继承。

Example of setting an environment variable with launchd:

使用launchd设置环境变量的示例:

echo setenv REPLACE_WITH_VAR REPLACE_WITH_VALUE | launchctl

Now, launch your GUI app that uses the variable, and voila!

现在,启动使用该变量的GUI应用程序,瞧!

To work around the fact that ~/.launchd.conf does not work, you can put the following script in ~/Library/LaunchAgents/local.launchd.conf.plist:

围绕这个事实工作。conf不工作,您可以将下列脚本放入~/Library/LaunchAgents/local.launchd.conf.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>local.launchd.conf</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>launchctl &lt; ~/.launchd.conf</string>    
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

Then you can put setenv REPLACE_WITH_VAR REPLACE_WITH_VALUE inside ~/.launchd.conf, and it will be executed at each login.

然后,可以将setenv REPLACE_WITH_VAR REPLACE_WITH_VALUE放入~/.launchd中。conf,它将在每次登录时执行。

Note that, when piping a command list into launchctl in this fashion, you will not be able to set environment variables with values containing spaces. If you need to do so, you can call launchctl as follows: launchctl setenv MYVARIABLE "QUOTE THE STRING".

注意,当以这种方式向launchctl发送命令列表时,您将无法设置包含空格的值的环境变量。如果您需要这样做,您可以调用launchctl: launchctl setenv MYVARIABLE“引用字符串”。

Also, note that other programs that run at login may execute before the launchagent, and thus may not see the environment variables it sets.

另外,请注意在登录时运行的其他程序可能在launchagent之前执行,因此可能看不到它设置的环境变量。

#3


12  

I think what the OP is looking for is a simple, windows-like solution.

我认为OP正在寻找的是一个简单的、类似windows的解决方案。

here ya go:

这丫去:

https://www.macupdate.com/app/mac/14617/rcenvironment

https://www.macupdate.com/app/mac/14617/rcenvironment

#4


7  

You can read up on linux, which is pretty close to what Mac OS X is. Or you can read up on BSD Unix, which is a little closer. For the most part, the differences between Linux and BSD don't amount to much.

你可以在linux上阅读,这与Mac OS X非常接近。或者您可以在BSD Unix上阅读,这是更近一些的。在大多数情况下,Linux和BSD之间的差异并不大。

/etc/profile are system environment variables.

/etc/profile是系统环境变量。

~/.profile are user-specific environment variables.

~ /。概要文件是用户特定的环境变量。

"where should I set my JAVA_HOME variable?"

“我应该在哪里设置我的JAVA_HOME变量?”

  • Do you have multiple users? Do they care? Would you mess some other user up by changing a /etc/profile?
  • 您有多个用户吗?他们关心吗?你会通过改变/etc/profile来搅乱其他用户吗?

Generally, I prefer not to mess with system-wide settings even though I'm the only user. I prefer to edit my local settings.

一般来说,即使我是唯一的用户,我也不愿意破坏整个系统的设置。我更喜欢编辑本地设置。

#5


5  

For GUI apps, you'll have to create and edit ~/.MacOSX/environment.plist. More details here. You will need to log out for these to take effect. I'm not sure if they also affect applications launched from Terminal, but I assume they would.

对于GUI应用程序,你必须创建和编辑~/. macosx /environment.plist。更多细节在这里。您将需要注销这些才能生效。我不确定它们是否也影响了从终端启动的应用程序,但我认为它们会。

For apps launched from Terminal, you can also edit the ~/.profile file.

对于从终端启动的应用程序,你也可以编辑~/。概要文件。

#6


3  

Just open the ~/.profile file, via nano in Terminal and type there :

只是打开~ /。概要文件,通过nano终端和类型:

export PATH=whatever/you/want:$PATH

Save this file (cmd+X and Y). After that please logout/login again or just open a new tab in Terminal and try use your new variable.

保存此文件(cmd+X和Y)。在此之后,请重新登出/登入,或在终端打开一个新标签,尝试使用你的新变量。

PLEASE DON'T forget to add ":$PATH" after whatever/you/want, otherwise you'll erase all paths in PATH variable, which were there before that.

请不要忘记添加“:$PATH”,在任何/你/需要之后,否则你将删除PATH变量中的所有路径,在此之前。

#7


3  

Synchronize OS X environment variables for command line and GUI applications from a single source with osx-env-sync.

同步OS X环境变量的命令行和GUI应用程序从一个单一来源与osx-env同步。

I also posted an answer to a related question here.

我也在这里发布了一个相关问题的答案。

#8


0  

If you want to change environment variables permanently on El Capitan, set them in /etc/paths. Note, this file is read-only by default, so you'll have to chmod for write permissions.

如果您想要永久地更改El Capitan上的环境变量,请将它们设置为/etc/paths。注意,这个文件默认是只读的,所以您需要chmod来获得写权限。

#1


132  

There's no need for duplication. You can set environment variables used by launchd (and child processes, i.e. anything you start from Spotlight) using launchctl setenv.

没有必要重复。您可以使用launchctl setenv来设置launchd(和子进程,即您从聚光灯开始的任何东西)所使用的环境变量。

For example, if you want to mirror your current path in launchd after setting it up in .bashrc or wherever:

例如,如果您想要在设置为.bashrc或其他任何地方之后,在launchd中镜像您当前的路径:

PATH=whatever:you:want
launchctl setenv PATH $PATH

Environment variables are not automatically updated in running applications. You will need to relaunch applications to get the updated environment variables (although you can just set variables in your shell, e.g. PATH=whatever:you:want; there's no need to relaunch the terminal).

在运行的应用程序中,环境变量不会自动更新。您需要重新启动应用程序以获得更新的环境变量(尽管您可以在shell中设置变量,例如PATH=您:想要的);没有必要重新启动终端。

#2


272  

There are several places where you can set environment variables.

有几个地方可以设置环境变量。

  • ~/.profile: use this for variables you want to set in all programs launched from the terminal (note that, unlike on Linux, all shells opened in Terminal.app are login shells).
  • ~ /。配置文件:将此用于您想要从终端启动的所有程序中设置的变量(注意,与Linux不同的是,所有shell都在终端中打开。应用程序登录shell)。
  • ~/.bashrc: this is invoked for shells which are not login shells. Use this for aliases and other things which need to be redefined in subshells, not for environment variables that are inherited.
  • ~ /。bashrc:这是调用没有登录shell的shell。用于别名和其他需要在子shell中重新定义的东西,而不是用于继承的环境变量。
  • /etc/profile: this is loaded before ~/.profile, but is otherwise equivalent. Use it when you want the variable to apply to terminal programs launched by all users on the machine (assuming they use bash).
  • /etc/profile:这是在~/之前加载的。配置文件,但在其他方面是等同的。当您希望变量应用于机器上所有用户启动的终端程序时(假设它们使用bash),请使用它。
  • ~/.MacOSX/environment.plist: this is read by loginwindow on login. It applies to all applications, including GUI ones, except those launched by Spotlight in 10.5 (not 10.6). It requires you to logout and login again for changes to take effect. This file is no longer supported as of OS X 10.8.
  • ~ / .MacOSX /环境。plist:登录时通过loginwindow读取。它适用于所有的应用程序,包括GUI的应用程序,除了那些在10.5(不是10.6)中发布的应用程序。它要求您注销并重新登录,以便更改生效。这个文件不再支持OS X 10.8。
  • your user's launchd instance: this applies to all programs launched by the user, GUI and CLI. You can apply changes at any time by using the setenv command in launchctl. In theory, you should be able to put setenv commands in ~/.launchd.conf, and launchd would read them automatically when the user logs in, but in practice support for this file was never implemented. Instead, you can use another mechanism to execute a script at login, and have that script call launchctl to set up the launchd environment.
  • 您的用户的launchd实例:这适用于由用户、GUI和CLI发起的所有程序。您可以在任何时候使用launchctl中的setenv命令来应用更改。理论上,您应该能够将setenv命令放在~/.launchd中。conf和launchd会在用户登录时自动读取它们,但是在实践中对该文件的支持从未实现。相反,您可以使用另一种机制来在登录时执行脚本,并使用该脚本调用launchctl来设置launchd环境。
  • /etc/launchd.conf: this is read by launchd when the system starts up and when a user logs in. They affect every single process on the system, because launchd is the root process. To apply changes to the running root launchd you can pipe the commands into sudo launchctl.
  • /etc/launchd.conf:当系统启动时,当用户登录时,就可以读取。它们影响系统中的每一个进程,因为launchd是根进程。要将更改应用到运行的root launchd,您可以将命令输入sudo launchctl。

The fundamental things to understand are:

最基本的理解是:

  • environment variables are inherited by a process's children at the time they are forked.
  • 环境变量是由进程的子节点在它们被分叉时继承的。
  • the root process is a launchd instance, and there is also a separate launchd instance per user session.
  • 根进程是一个launchd实例,并且每个用户会话也有一个单独的launchd实例。
  • launchd allows you to change its current environment variables using launchctl; the updated variables are then inherited by all new processes it forks from then on.
  • launchd允许您使用launchctl更改其当前环境变量;然后,更新后的变量将由它从那时开始的所有新进程继承。

Example of setting an environment variable with launchd:

使用launchd设置环境变量的示例:

echo setenv REPLACE_WITH_VAR REPLACE_WITH_VALUE | launchctl

Now, launch your GUI app that uses the variable, and voila!

现在,启动使用该变量的GUI应用程序,瞧!

To work around the fact that ~/.launchd.conf does not work, you can put the following script in ~/Library/LaunchAgents/local.launchd.conf.plist:

围绕这个事实工作。conf不工作,您可以将下列脚本放入~/Library/LaunchAgents/local.launchd.conf.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>local.launchd.conf</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>launchctl &lt; ~/.launchd.conf</string>    
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

Then you can put setenv REPLACE_WITH_VAR REPLACE_WITH_VALUE inside ~/.launchd.conf, and it will be executed at each login.

然后,可以将setenv REPLACE_WITH_VAR REPLACE_WITH_VALUE放入~/.launchd中。conf,它将在每次登录时执行。

Note that, when piping a command list into launchctl in this fashion, you will not be able to set environment variables with values containing spaces. If you need to do so, you can call launchctl as follows: launchctl setenv MYVARIABLE "QUOTE THE STRING".

注意,当以这种方式向launchctl发送命令列表时,您将无法设置包含空格的值的环境变量。如果您需要这样做,您可以调用launchctl: launchctl setenv MYVARIABLE“引用字符串”。

Also, note that other programs that run at login may execute before the launchagent, and thus may not see the environment variables it sets.

另外,请注意在登录时运行的其他程序可能在launchagent之前执行,因此可能看不到它设置的环境变量。

#3


12  

I think what the OP is looking for is a simple, windows-like solution.

我认为OP正在寻找的是一个简单的、类似windows的解决方案。

here ya go:

这丫去:

https://www.macupdate.com/app/mac/14617/rcenvironment

https://www.macupdate.com/app/mac/14617/rcenvironment

#4


7  

You can read up on linux, which is pretty close to what Mac OS X is. Or you can read up on BSD Unix, which is a little closer. For the most part, the differences between Linux and BSD don't amount to much.

你可以在linux上阅读,这与Mac OS X非常接近。或者您可以在BSD Unix上阅读,这是更近一些的。在大多数情况下,Linux和BSD之间的差异并不大。

/etc/profile are system environment variables.

/etc/profile是系统环境变量。

~/.profile are user-specific environment variables.

~ /。概要文件是用户特定的环境变量。

"where should I set my JAVA_HOME variable?"

“我应该在哪里设置我的JAVA_HOME变量?”

  • Do you have multiple users? Do they care? Would you mess some other user up by changing a /etc/profile?
  • 您有多个用户吗?他们关心吗?你会通过改变/etc/profile来搅乱其他用户吗?

Generally, I prefer not to mess with system-wide settings even though I'm the only user. I prefer to edit my local settings.

一般来说,即使我是唯一的用户,我也不愿意破坏整个系统的设置。我更喜欢编辑本地设置。

#5


5  

For GUI apps, you'll have to create and edit ~/.MacOSX/environment.plist. More details here. You will need to log out for these to take effect. I'm not sure if they also affect applications launched from Terminal, but I assume they would.

对于GUI应用程序,你必须创建和编辑~/. macosx /environment.plist。更多细节在这里。您将需要注销这些才能生效。我不确定它们是否也影响了从终端启动的应用程序,但我认为它们会。

For apps launched from Terminal, you can also edit the ~/.profile file.

对于从终端启动的应用程序,你也可以编辑~/。概要文件。

#6


3  

Just open the ~/.profile file, via nano in Terminal and type there :

只是打开~ /。概要文件,通过nano终端和类型:

export PATH=whatever/you/want:$PATH

Save this file (cmd+X and Y). After that please logout/login again or just open a new tab in Terminal and try use your new variable.

保存此文件(cmd+X和Y)。在此之后,请重新登出/登入,或在终端打开一个新标签,尝试使用你的新变量。

PLEASE DON'T forget to add ":$PATH" after whatever/you/want, otherwise you'll erase all paths in PATH variable, which were there before that.

请不要忘记添加“:$PATH”,在任何/你/需要之后,否则你将删除PATH变量中的所有路径,在此之前。

#7


3  

Synchronize OS X environment variables for command line and GUI applications from a single source with osx-env-sync.

同步OS X环境变量的命令行和GUI应用程序从一个单一来源与osx-env同步。

I also posted an answer to a related question here.

我也在这里发布了一个相关问题的答案。

#8


0  

If you want to change environment variables permanently on El Capitan, set them in /etc/paths. Note, this file is read-only by default, so you'll have to chmod for write permissions.

如果您想要永久地更改El Capitan上的环境变量,请将它们设置为/etc/paths。注意,这个文件默认是只读的,所以您需要chmod来获得写权限。