macos Mac OS X 中的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/603785/
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
Environment variables in Mac OS X
提问by Abdullah Jibaly
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 这样的应用程序访问变量。另外,我希望我不必重新启动/注销才能使其生效。
采纳答案by Matt Curtis
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
.
For example, if you want to mirror your current path in launchd after setting it up in .bashrc
or wherever:
例如,如果您想在 launchd 中.bashrc
或其他地方设置它后镜像当前路径:
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=whatever:you:want
;无需重新启动终端)。
回答by LaC
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).~/.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 thesetenv
command inlaunchctl
. In theory, you should be able to putsetenv
commands in~/.launchd.conf
, andlaunchd
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 calllaunchctl
to set up thelaunchd
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 intosudo launchctl
.
~/.profile
:将此用于要在从终端启动的所有程序中设置的变量(请注意,与 Linux 不同,在 Terminal.app 中打开的所有 shell 都是登录 shell)。~/.bashrc
:这是为不是登录 shell 的 shell 调用的。将此用于别名和其他需要在子 shell 中重新定义的内容,而不是用于继承的环境变量。/etc/profile
: 这是在 ~/.profile 之前加载的,但在其他方面是等效的。当您希望变量应用于机器上所有用户启动的终端程序时使用它(假设他们使用 bash)。~/.MacOSX/environment.plist
:这是由登录窗口在登录时读取的。它适用于所有应用程序,包括 GUI 应用程序,但 Spotlight 在 10.5(不是 10.6)中启动的应用程序除外。它需要您注销并重新登录才能使更改生效。自 OS X 10.8 起不再支持此文件。- 您的用户
launchd
实例:这适用于用户、GUI 和 CLI 启动的所有程序。您可以随时使用 中的setenv
命令应用更改launchctl
。理论上,您应该能够将setenv
命令放入~/.launchd.conf
,并且launchd
会在用户登录时自动读取它们,但实际上从未实现对此文件的支持。相反,您可以使用另一种机制在登录时执行脚本,并调用该脚本launchctl
来设置launchd
环境。 /etc/launchd.conf
: 这在系统启动和用户登录时由launchd读取。它们影响系统上的每个进程,因为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 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.
- 环境变量在分叉时由进程的子进程继承。
- root 进程是一个launchd 实例,每个用户会话还有一个单独的launchd 实例。
- 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
:
要解决不起作用的事实~/.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 < ~/.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
inside ~/.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 "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.
另请注意,登录时运行的其他程序可能会在启动代理之前执行,因此可能看不到它设置的环境变量。
回答by Tom Teman
I think what the OP is looking for is a simple, windows-like solution.
我认为 OP 正在寻找的是一个简单的、类似 windows 的解决方案。
here ya go:
给你:
回答by S.Lott
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.
~/.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
?
- 你有多个用户吗?他们在乎吗?您会通过更改 a 来弄乱其他用户
/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.
通常,即使我是唯一的用户,我也不想弄乱系统范围的设置。我更喜欢编辑我的本地设置。
回答by JW.
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.
对于从终端启动的应用程序,您还可以编辑 ~/.profile 文件。
回答by Migele
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). 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 变量中的所有路径,这些路径在此之前就存在。
回答by Ersin Er
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.
我还张贴回答一个相关的问题在这里。
回答by IgorGanapolsky
If you want to change environment variables permanently on macOS, set them in /etc/paths
. Note, this file is read-only by default, so you'll have to chmodfor write permissions.
如果要在 macOS 上永久更改环境变量,请将它们设置在/etc/paths
. 请注意,默认情况下此文件是只读的,因此您必须chmod才能获得写入权限。
回答by Raining
For 2020 Mac OS X Catalina users:
对于 2020 Mac OS X Catalina 用户:
Forget about other useless answers, here only two steps needed:
忘记其他无用的答案,这里只需要两个步骤:
Create a file with the naming convention: priority-appname. Then copy-paste the path you want to add to
PATH
.E.g.
80-vscode
with content/Applications/Visual Studio Code.app/Contents/Resources/app/bin/
in my case.Move that file to
/etc/paths.d/
. Don't forget to open a new tab(new session) in the Terminal and typeecho $PATH
to check that your path is added!
使用命名约定创建一个文件:priority-appname。然后将要添加的路径复制粘贴到
PATH
.例如在我的情况下
80-vscode
有内容/Applications/Visual Studio Code.app/Contents/Resources/app/bin/
。将该文件移动到
/etc/paths.d/
. 不要忘记在终端中打开一个新选项卡(新会话)并键入echo $PATH
以检查您的路径是否已添加!
Notice: this method only appendsyour path to PATH
.
注意:此方法仅将您的路径附加到PATH
.