0
subprocess.call(["echo",instance.Username])  # => works .


subprocess.call(["username=",instance.Username])  # => error
subprocess.Call(["username","=",instance.Username])  # => error

subprocess.call(["username=",instance.Username],shell=True)
# => no Error but when i do echo $username there is nothing inside..

would you help me, I just want to put the value of instance.Username in a bash variable.

Thank you.

2
  • 1
    You'll have to give more details. Exactly what are you going to do with that "bash variable", and where? Commented Jul 3, 2016 at 10:32
  • I will need it to execute another bash script.. for the moment, I just want to put the value of instance.Username in a bash variable that i can display in my terminal Commented Jul 3, 2016 at 10:34

1 Answer 1

2

You'll want to pass it as an environment variable to subprocess.call() like so:

env = {
    'username': instance.Username,
}

subprocess.call(['./your_script.sh'], env=env)

This will make the environment variable $username available to your bash script.

Remember to enclose the bash command in a list, run the script with ./, and start the script with the correct shebang, (all things I forgot to do while writing this).

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this : env = { 'username': instance.Username, } subprocess.call(['echo'], env=env) it return nothing.
Of course it won't return anything. You need subprocess.check_output for that.
sorry, I wanted to say that when i do subprocess.call(['echo'], env=env , it gives me a blank line !!
Of course; you're not echoing anything. If you want to echo something from the environment, you'd need to name it as normal. subprocess.call(['echo', '$username'], env={'username':'AdamBarnes'}).
I messed up my last comment. The command should have been subprocess.call('echo $username', env={'username':'AdamBarnes'}, shell=True). Note that you won't need shell=True when calling your script, and you shouldn't use shell=True if you're ever passing user-supplied data to subprocess.call().

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.