5

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..

2 Answers 2

12

Inject the value into the environment with os.environ.

os.environ['var'] = "foo_bar"
subprocess.call(['echo $var'], shell=True)
Sign up to request clarification or add additional context in comments.

1 Comment

Also look at the env parameter to subprocess.Popen and subprocess.call.
0

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).

Comments

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.