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

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


来自Windows的背景,设置和修改环境变量非常容易(只要去系统属性>高级>环境变量),在Mac OS 10.5上似乎没有那么直接。大多数引用都说我应该更新/etc/profile或~/.profile。这些变量是否等同于系统变量和用户变量?例如,我应该在哪里设置JAVA_HOME变量?


编辑:

我希望能够从终端以及像Eclipse这样的应用程序访问变量。另外,我希望我不需要重新启动/注销才能使其生效。


当前回答

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

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

其他回答

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

对于从Terminal启动的应用程序,您还可以编辑~/。概要文件。

如果要永久修改macOS环境变量,请将环境变量设置为“/etc/paths.”注意,默认情况下这个文件是只读的,所以您必须使用chmod来获得写权限。

我写了一个工具来方便地管理macOS应用程序的环境变量。

https://github.com/yuezk/macenv

您可以使用macenv set来设置环境变量,例如:

macenv set JAVA_HOME /path/to/java/home

在底层,它调用launchctl setenv来设置环境变量,同时将环境变量保存到~/.launchd.conf中,并注册一个自动启动服务来在操作系统重新启动时加载环境变量。

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

来吧:

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

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

~/.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). ~/.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. /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). ~/.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. 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. /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.

需要理解的基本事情是:

环境变量在fork时由进程的子进程继承。 根进程是一个launchd实例,每个用户会话还有一个单独的launchd实例。 Launchd允许您使用launchctl更改其当前环境变量;更新后的变量将被它从此以后派生的所有新进程继承。

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

echo setenv REPLACE_WITH_VAR REPLACE_WITH_VALUE | launchctl

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

为了解决~/.launchd.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>

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

请注意,当以这种方式将命令列表管道到launchctl中时,您将不能设置包含空格的值的环境变量。如果你需要这样做,你可以调用launchctl,如下所示:

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