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

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


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


编辑:

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


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

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


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

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

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

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

您是否有多个用户?他们在乎吗?你会因为修改/etc/profile而把其他用户搞得一团糟吗?

一般来说,即使我是唯一的用户,我也不喜欢打乱系统范围的设置。我更喜欢编辑我的本地设置。


没有必要重复。您可以使用launchctl setenv设置launchd(和子进程,即从Spotlight启动的任何进程)使用的环境变量。

例如,如果你想在.bashrc或其他文件中设置启动后镜像当前路径:

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

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


我认为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,如下所示:

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


只需要打开~/。配置文件,通过nano在终端,并键入:

export PATH=whatever/you/want:$PATH

保存该文件(cmd+X和Y)。 之后,请注销/登录再次或只是在终端打开一个新选项卡,并尝试使用您的新变量。

请不要忘记在/you/想要的东西后面加上“:$PATH”,否则你会删除PATH变量中之前的所有路径。


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

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


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


对于2020年Mac OS X Catalina用户:

忘掉其他没用的答案吧,这里只需要两个步骤:

创建一个命名约定为priority-appname的文件。然后复制粘贴要添加到path中的路径。 例如80-vscode与内容/应用程序/Visual Studio代码。在我的例子中,app/Contents/Resources/app/bin/。 将该文件移动到/etc/paths.d/。不要忘记在终端中打开一个新选项卡(新会话)并输入echo $PATH来检查您的路径是否被添加!

注意:此方法仅将路径追加到path。


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

https://github.com/yuezk/macenv

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

macenv set JAVA_HOME /path/to/java/home

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