How do I pass a Python Variable to Bash?

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="./ 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?

call external program in python, watch output for specific text then take action


Valgrind 'noise', what does it mean?
foo="$(scriptthatprintsany thing)" 
That's it. How to send a EML file as email using python script to list of emails one at a time?print. How to retrieve value from etc/sysconfig in PythonOr sys.stdout.write(). Displaying an image from the tmp directoryOr the like. How do they read clusters/cylinders/sectors from the disk?If the script isn't executable then you'll need to specify the interpreter explicitly.. Use OpenBSD's malloc, realloc and free in my program
foo="$(python scriptthatprintsany" 
Strange results while measuring delta time on Linux


Your desired form works just fine:.
$ cat > #!/usr/bin/python import sys print sys.argv[1] $ chmod +x  $ foo=`./ 42` $ echo $foo 42 $  
so presumably the ways in which you had tried printing were incorrect. (This is quite independent from using the older-style backquotes, or newer-style constructs such as $()). If you still have this problem, must you show us the minimal case of Python code this reproduces it, to guidance us guidance you?.


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=`any  command` 
or .
foo=$(any  command) 
Normally you wrap this in double quotes so you must have spaces in your output. It need be double quotes as stuff inside single quotes will not be executed. .

84 out of 100 based on 59 user ratings 774 reviews