导出不能在Mac上永久导出变量 [英] export can't export variables permanently on Mac
问题描述
我想全局设置一个环境变量,所以我将其添加到~/.bashrc
:
export MONGO_HOME=~/mongo
它在此命令控制台中有效,但是当我打开另一个命令控制台时,它无效.刚输入source ~/.bashrc
后就可以使用,因此我也将此代码添加到了~/.bash_profile
和~/.profile
中,但结果相同.
谁能告诉我为什么会发生这种情况,以及如何永久保存此变量?我在Mac上.
Scott Crook的答案提供了有效的解决方案./p>
这里有一些背景:
-
在OS X上,由
Terminal.app
创建的所有Bash实例都是 login shell(不同于大多数Linux系统上由终端创建的实例). -
交互式Bash登录外壳程序在用户级别上获取存在的以下文件的 first :
~/.bash_profile
,~/.bash_login
,~/.profile
;作为交互式 login 外壳程序,但是,它们不也提供源代码~/.bashrc
. -
因此,要确保将
~/.bashrc
加载到 all 交互式外壳中,则应将其作为语句即可.请注意,即使您全局说 ,上述操作也只会生效:
- 用于交互式 shell实例(尤其不适用于幕后应用程序创建的shell).
- 当前用户
要使环境变量定义对 all 所有用户生效,请将其放置在
/etc/profile
中,但要注意的是,这仅对 interactive shell有效I want to set a env variable globally, so I add this to
~/.bashrc
:export MONGO_HOME=~/mongo
It works in this command console, but when I open another command console, it doesn't work; just after I type
source ~/.bashrc
it works, so I add this code to~/.bash_profile
and~/.profile
also, but same result.Can anyone tell me why this happens and how can I save this variable permanently? I'm on Mac.
解决方案Scott Crook's answer provides an effective solution.
Here's some background:
On OS X, all Bash instances created by
Terminal.app
are login shells (unlike the instances created by terminals on most Linux systems).Interactive Bash login shells, at the user level, source the first of the following files that exists:
~/.bash_profile
,~/.bash_login
,~/.profile
; as interactive login shells, however, they do not also source~/.bashrc
.Thus, to ensure that
~/.bashrc
is loaded in all interactive shells, it should be sourced from~/.bash_profile
, as Scott Crook's answer recommends.
Adding your
export
statements to~/.bash_profile
should have taken effect in any shell tabs/windows opened thereafter. To troubleshoot, simply add anecho "Loading <filename>"
statement to the beginning of the initialization file(s) of interest.Note that even though you say globally, the above only takes effect:
- for interactive shell instances (notably not for shells created by applications behind the scenes).
- for the current user
To have environment-variable definitions take effect for all users, place them in
/etc/profile
, but not that, again, this will only take effect for interactive shells.这篇关于导出不能在Mac上永久导出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!