Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a Python Variable to Bash?

How would I pass a Python variable to the Bash shell? It should work like this: foo="./RetVar.py 42"

Replace the double-quotes with `s

I have tried printing and sys.exiting the result, but to no avail. How would I accomplish my goal?

like image 755
user336537 Avatar asked Apr 13 '26 17:04

user336537


2 Answers

foo="$(scriptthatprintssomething)"

That's it. print. Or sys.stdout.write(). Or the like. If the script isn't executable then you'll need to specify the interpreter explicitly.

foo="$(python scriptthatprintssomething.py)"
like image 86
Ignacio Vazquez-Abrams Avatar answered Apr 16 '26 06:04

Ignacio Vazquez-Abrams


In bash both ``cmd\ and $(cmd) will be replaced by the output of the command. This allows you to assign the output of a program to a variable like

foo=`some command`

or

foo=$(some command)

Normally you wrap this in double quotes so you can have spaces in your output. It must be double quotes as stuff inside single quotes will not be executed.

like image 44
seamus Avatar answered Apr 16 '26 07:04

seamus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!