2

When I try to access the "HISTFILE" env var within a program, it isn't there

$ 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'

But if I set it directly, it is:

$ 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'

It seems to be hidden for some reason. Why is that? Is there any way to access this var within my programs?

2
  • That explains somewhat, but doesn't explain how to access HISTFILE in a program (if thats even possible)
    – Drew
    Jul 13, 2016 at 6:11
  • @Drew: You need to export the shell variable for it to become an environment variable, which will then be inherited by child processes.
    – camh
    Jul 13, 2016 at 6:26

1 Answer 1

2

You will need to either type export HISTFILE on the command line prior to starting the python interpreter (or running your python script) or add export HISTFILE to your .bashrc file so that it is automatically exported when you login.

Not the answer you're looking for? Browse other questions tagged .