我看到在Mac OS中存储环境的奇怪问题。
我在~/.bash_profile
中设置了自定义环境
出口MYENV =用户
然后运行. ~/.bash_profile
,然后使用
printenv
,然后我可以在列表中看到MYENV = user。
如果我关闭终端并重新打开并执行printenv
,则在列表中仍看不到MYENV
,我仍然可以在~/.bash_profile
中看到导出MYENV = user。对我来说似乎很奇怪。
我正在使用 Mac os High Sierra 10.13.6 。 有人可以告诉我我在做什么错吗?
答案 0 :(得分:0)
请注意,〜/ .bash_profile 仅用于登录Shell。在手册页中:
When bash is invoked as an interactive login shell, or as a non-interactive shell
with the --login option, it first reads and executes commands from the file
/etc/profile, if that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and exe-
cutes commands from the first one that exists and is readable. The --noprofile
option may be used when the shell is started to inhibit this behavior.
因此,如果您的终端不是使用-l
,--login
或$0
带有连字符的前导启动外壳程序,则它将不是登录外壳程序,因此不会读取〜/ .bash_profile 。如果希望外壳读取配置脚本,则可能需要重新配置终端如何启动外壳。
另一方面,〜/ .bashrc 始终由交互式外壳读取。因此,如果将导出文件放在该脚本中,它应该可以完成您的期望。当然对我有用。您回复了Amila,它对您不起作用。因此,我建议进行一个简单的实验。打开两个终端窗口。一次编辑〜/ .bashrc 并添加以下两行:
echo running .bashrc
export WTF=abc
在另一个窗口中,只需运行bash
。它应该回显该消息,并且echo $WTF
应该打印abc
。现在打开一个新的终端窗口。如果您没有看到该消息并且env var不存在,则说明有某些内容禁止读取该配置脚本。可能正在使用--norc
标志运行该外壳程序。
答案 1 :(得分:-1)
~/.bash_profile
在初始命令提示符返回给用户之前执行,这意味着在重新登录之后。尝试将环境变量添加到~/.bashrc
中。