简体   繁体   English

Mac OS X 中的环境变量

[英]Environment variables in Mac OS X

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.来自 Windows 背景,设置和修改环境变量非常容易(只需转到“系统属性”>“高级”>“环境变量”),在 Mac OS 10.5 上似乎并不那么简单。 Most references say I should update /etc/profile or ~/.profile.大多数参考资料都说我应该更新 /etc/profile 或 ~/.profile。 Are those the equivalent of System Variables and User Variables?那些等同于系统变量和用户变量吗? For example, where should I set my JAVA_HOME variable?例如,我应该在哪里设置我的JAVA_HOME变量?


EDIT:编辑:

I want to be able to access the variable from the terminal as well as an app like Eclipse.我希望能够从终端以及像 Eclipse 这样的应用程序访问变量。 Also, I hope I don't have to restart/logout to make this take effect.另外,我希望我不必重新启动/注销才能使其生效。

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). ~/.profile :将此用于要在从终端启动的所有程序中设置的变量(请注意,与 Linux 不同,在 Terminal.app 中打开的所有 shell 都是登录 shell)。
  • ~/.bashrc : this is invoked for shells which are not login shells. ~/.bashrc :这是为非登录 shell 的 shell 调用的。 Use this for aliases and other things which need to be redefined in subshells, not for environment variables that are inherited.将此用于别名和其他需要在子 shell 中重新定义的内容,而不是用于继承的环境变量。
  • /etc/profile : this is loaded before ~/.profile, but is otherwise equivalent. /etc/profile :它在 ~/.profile 之前加载,但在其他方面是等效的。 Use it when you want the variable to apply to terminal programs launched by all users on the machine (assuming they use bash).当您希望变量应用于机器上所有用户启动的终端程序时使用它(假设他们使用 bash)。
  • ~/.MacOSX/environment.plist : this is read by loginwindow on login. ~/.MacOSX/environment.plist :登录时由 loginwindow 读取。 It applies to all applications, including GUI ones, except those launched by Spotlight in 10.5 (not 10.6).它适用于所有应用程序,包括 GUI 应用程序,但 Spotlight 在 10.5(不是 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.自 OS X 10.8 起不再支持此文件。
  • your user's launchd instance: this applies to all programs launched by the user, GUI and CLI.您用户的launchd实例:这适用于用户、GUI 和 CLI 启动的所有程序。 You can apply changes at any time by using the setenv command in launchctl .您可以随时使用launchctlsetenv命令应用更改。 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.理论上,您应该能够将setenv命令放在~/.launchd.conf ,并且launchd会在用户登录时自动读取它们,但实际上从未实现对此文件的支持。 Instead, you can use another mechanism to execute a script at login, and have that script call launchctl to set up the launchd environment.相反,您可以使用另一种机制在登录时执行脚本,并让该脚本调用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. /etc/launchd.conf :它在系统启动和用户登录时由 launchd 读取。它们影响系统上的每个进程,因为 launchd 是根进程。 To apply changes to the running root launchd you can pipe the commands into sudo launchctl .要将更改应用于正在运行的 root launchd,您可以将命令通过管道sudo launchctlsudo 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 allows you to change its current environment variables using launchctl ; launchd 允许您使用launchctl更改其当前环境变量; the updated variables are then inherited by all new processes it forks from then on.更新的变量然后由它从那时起分叉的所有新进程继承。

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 :要解决~/.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>

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.请注意,以这种方式将命令列表传送到 launchctl 时,您将无法使用包含空格的值设置环境变量。 If you need to do so, you can call launchctl as follows: launchctl setenv MYVARIABLE "QUOTE THE STRING" .如果您需要这样做,您可以按如下方式调用 launchctl: launchctl setenv MYVARIABLE "QUOTE THE STRING"

Also, note that other programs that run at login may execute before the launchagent, and thus may not see the environment variables it sets.另请注意,登录时运行的其他程序可能会在启动代理之前执行,因此可能看不到它设置的环境变量。

There's no need for duplication.没有必要重复。 You can set environment variables used by launchd (and child processes, ie anything you start from Spotlight) using launchctl setenv .您可以使用launchctl setenv设置由 launchd(和子进程,即您从 Spotlight 启动的任何程序)使用的环境变量。

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, eg PATH=whatever:you:want ; there's no need to relaunch the terminal).您将需要重新启动应用程序以获取更新的环境变量(尽管您可以只在 shell 中设置变量,例如PATH=whatever:you:want ;无需重新启动终端)。

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

You can read up on linux, which is pretty close to what Mac OS X is.您可以在 linux 上阅读,这与 Mac OS X 非常接近。 Or you can read up on BSD Unix, which is a little closer.或者你可以在 BSD Unix 上阅读,它更接近一些。 For the most part, the differences between Linux and BSD don't amount to much.在大多数情况下,Linux 和 BSD 之间的差异并不大。

/etc/profile are system environment variables. /etc/profile是系统环境变量。

~/.profile are user-specific environment variables. ~/.profile是用户特定的环境变量。

"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.我更喜欢编辑我的本地设置。

For GUI apps, you'll have to create and edit ~/.MacOSX/environment.plist .对于 GUI 应用程序,您必须创建和编辑~/.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.我不确定它们是否也会影响从终端启动的应用程序,但我认为它们会。

For apps launched from Terminal, you can also edit the ~/.profile file.对于从终端启动的应用程序,您还可以编辑 ~/.profile 文件。

Just open the ~/.profile file, via nano in Terminal and type there :只需打开~/.profile文件,通过终端中的nano在那里输入:

export PATH=whatever/you/want:$PATH

Save this file (cmd+X and Y).保存此文件(cmd+X 和 Y)。 After that please logout/login again or just open a new tab in Terminal and try use your new variable.之后,请再次注销/登录或在终端中打开一个新选项卡并尝试使用您的新变量。

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 变量中的所有路径,这些路径在此之前存在。

Synchronize OS X environment variables for command line and GUI applications from a single source with osx-env-sync .使用osx-env-sync从单一来源同步命令行和 GUI 应用程序的 OS X 环境变量。

I also posted an answer to a related question here .我还张贴回答一个相关的问题在这里

I write a tool to make it easy to manage the environment variables for macOS applications.我编写了一个工具来轻松管理 macOS 应用程序的环境变量。

https://github.com/yuezk/macenv https://github.com/yuezk/macenv

You can set the environment variable with ~/.macenv set , for example:您可以使用~/.macenv set环境变量,例如:

~/.macenv set JAVA_HOME /path/to/java/home

Under the hood, it calls launchctl setenv to set the environment variables, saves the environment variables to ~/.launchd.conf at the same time, and registers an auto-start service to load the environment variables when OS restarts.launchctl setenv ,它调用launchctl setenv来设置环境变量,同时将环境变量保存到~/.launchd.conf中,并注册一个自动启动服务来在操作系统重启时加载环境变量。

If you want to change environment variables permanently on macOS, set them in /etc/paths .如果要在 macOS 上永久更改环境变量,请在/etc/paths设置它们。 Note , this file is read-only by default, so you'll have to chmod for write permissions.请注意,默认情况下此文件是只读的,因此您必须chmod才能获得写入权限。

For 2020 Mac OS X Catalina users:对于 2020 Mac OS X Catalina 用户:

Forget about other useless answers, here only two steps needed:忘记其他无用的答案,这里只需要两个步骤:

  1. Create a file with the naming convention: priority-appname.使用命名约定创建一个文件:priority-appname。 Then copy-paste the path you want to add to PATH .然后将要添加的路径复制粘贴到PATH

    Eg 80-vscode with content /Applications/Visual Studio Code.app/Contents/Resources/app/bin/ in my case.例如80-vscode与内容/Applications/Visual Studio Code.app/Contents/Resources/app/bin/在我的情况下。

  2. Move that file to /etc/paths.d/ .将该文件移动到/etc/paths.d/ Don't forget to open a new tab(new session) in the Terminal and type echo $PATH to check that your path is added!不要忘记在终端中打开一个新选项卡(新会话)并输入echo $PATH以检查您的路径是否已添加!

Notice: this method only appends your path to PATH .注意:此方法仅您的路径附加PATH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM