Running a shell command from a variable in a shell script -


i programming game using linux.

i have shell script:

//run.sh     a="string1" b="string2" c="string3"  command_line="python ../file.py \"$a\" \"$b\" --flag1 ../file.txt --flag2 $c" echo "$command_line" $command_line 

//note ' \" ' intentionally

i want shell run command in command_line. reason command not work, if take string created , stored in command_line(the string echoed) , run through shell, program works fine.

any suggestions?

thank you

the embedded quotes in command_line treated literal characters; not quote value of $a when $command_line expanded. there isn't good, safe way execute value of variable command in posix shell.

if using bash or shell supports arrays, can try

options=( ../file.py "$a" "$b" --flag1 ../file.txt --flag2 $c ) echo "python ${options[@]}" python "${options[@]}" 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -