程序内未设置 HISTFILE 环境变量。我可以通过任何方式访问它吗?

程序内未设置 HISTFILE 环境变量。我可以通过任何方式访问它吗?

当我尝试访问程序中的“HISTFILE”环境变量时,它不存在

$ echo $HISTFILE
/Users/drewgross/.bash_history
$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['HISTFILE']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'HISTFILE'

但如果我直接设置的话是:

$ HISTFILE=wat python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['HISTFILE']
'wat'

由于某种原因,它似乎被隐藏了。这是为什么?有什么方法可以在我的程序中访问这个变量吗?

答案1

您需要输入导出历史文件在启动 python 解释器(或运行 python 脚本)之前在命令行上或添加导出历史文件到您的 .bashrc 文件,以便在您登录时自动导出。

相关内容