Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bash variables inside a Python code?

Tags:

python

bash

I wanna do this:

    import subprocess 
    subprocess.call(['var="foo_bar"'], shell=True)
    subprocess.call(['echo $var'], shell=True)

But when i will use the $var in the second line your value is losted.

Somebody know some say to solve this problem? This is a simple example, but a need to do that in a much more complex code here..

Tks..

like image 394
JonatasTeixeira Avatar asked May 26 '11 17:05

JonatasTeixeira


1 Answers

Inject the value into the environment with os.environ.

os.environ['var'] = "foo_bar"
subprocess.call(['echo $var'], shell=True)
like image 110
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 01:09

Ignacio Vazquez-Abrams