Running echo $HISTFILE in bash gives me /home/myname/.bash_history.
Now I'm trying to use Python to access this HISTFILE shell variable by doing the following:
import subprocess
x = subprocess.check_output('echo $HISTFILE', shell=True)
print(x)
However, this gives me a blank output.
- If
subprocess.check_outputalong withshell=Trueexecutes the command in a shell, why doesn't it have a HISTFILE variable that I can access? - How can I work my way around this?
asked Aug 20, 2020 at 19:05
magikarp
4601 gold badge10 silver badges24 bronze badges
1 Answer 1
Using subprocess seems to be overkill for the task, you can use os.environ to access environment variables.
import os
os.environ['HISTFILE']
answered Aug 20, 2020 at 19:10
Armin Primadi
7646 silver badges10 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
magikarp
That gives me a KeyError.
default
os.environ["HISTFILE"].$HISTFILElooks like any other environment variable but in reality it's a shell variable and you've to export it in order to access it usingos.environor child processes.