How to use bash variables inside a Python code?
I wanna do this:
开发者_Python百科 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..
Inject the value into the environment with os.environ
.
os.environ['var'] = "foo_bar"
subprocess.call(['echo $var'], shell=True)
You can't. You can use enviroment variables (os.environ
), and you can of course use everything you get passed as command line args (sys.argv
), but bash variables are bash variables and not Python variables. You'll have to find another way (perhaps we could suggest a way if you told us what exactly you want to do).
精彩评论