I have a shell script which has a piece of python code in it .. How can i share the variables form my shell script to the piece of python code
This is my code
#!/bin/sh
version="1"
output=$(python -c '
import sys;
import json;
if $version == 1:
print("yes")
')
echo $output
It gives syntax error : Invalid syntax.. How can I use the version variable in the python code
please Help:)
CodePudding user response:
Don't share variables. Pass arguments.
output=$(python -c '
import sys
if sys.argv[1] == "1":
print("yes")
' "$version")
CodePudding user response:
In Bash, variable expansion doesn't happen in single quote (') enclosed strings, only in double quote (") ones. So make sure to use double quotes to define your python code.
Note that this will force you to either use only 'inside python or to escape internal ".
So you can try this:
#!/bin/sh
version="1"
output=$(python -c "
import sys;
import json;
if $version == 1:
print('yes')
")
echo $output
