0

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.

  1. If subprocess.check_output along with shell=True executes the command in a shell, why doesn't it have a HISTFILE variable that I can access?
  2. How can I work my way around this?
3
  • 3
    os.environ["HISTFILE"]. Commented Aug 20, 2020 at 19:08
  • Note that for HISTFILE to show up in the environment (os.environ), it must first be exported: unix.stackexchange.com/questions/295555 I think it isn't by default. Commented Aug 20, 2020 at 19:13
  • $HISTFILE looks like any other environment variable but in reality it's a shell variable and you've to export it in order to access it using os.environ or child processes. Commented Aug 20, 2020 at 19:34

1 Answer 1

0

Using subprocess seems to be overkill for the task, you can use os.environ to access environment variables.

import os
os.environ['HISTFILE']
Sign up to request clarification or add additional context in comments.

1 Comment

That gives me a KeyError.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.