Bash and Double-Quotes passing to argv

arguments, bash, c

Solution

What is happening is that your nested quotes are literal and not parsed into separate arguments by the shell. The best way to handle this using `bash` is to use an array instead of a string:

args=('key1=value1', 'key2=value2 key3=value3')
prog.exe "${args[@]}"

The Bash FAQ50 has some more examples and use cases for dynamic commands.

Problem

I have re-purposed this example to keep it simple, but what I am trying to do is get a nested double-quote string as a single argv value when the bash shell executes it. Here is the script example: ``` set -x command1="key1=value1 \"key2=value2 key3=value3\"" command2="keyA=valueA keyB=valueB keyC=valueC" echo $command1 echo $command2 ``` the output is: ``` ++ command1='key1=value1 "key2=value2 key3=value3"' ++ command2='keyA=valueA keyB=valueB keyC=valueC' ++ echo key1=value1 '"key2=value2' 'key3=value3"' key1=value1 "key2=value2 key3=value3" ++ echo keyA=valueA keyB=valueB keyC=valueC keyA=valueA keyB=valueB keyC=valueC ``` I did test as well, that when you do everything on the command line, the nested quote message IS set as a single argv value. i.e. ``` prog.exe argument1 "argument2 argument3" argv[0] = prog.exe argv[1] = argument1 argv[2] = argument2 argument3 ``` Using the above example: ``` command1="key1=value1 \"key2=value2 key3=value3\"" ``` The error is, my argv is comming back like: ``` arg[1] = echo arg[2] = key1=value1 arg[3] = "key2=value2 arg[4] = key3=value3" ``` where I really want my argv[3] value to be "key2=value2 key3=value3" I noticed that debug (set -x) shows a single-quote at the points where my arguments get broken which kinda indicates that it is thinking about the arguments at these break point...just not sure. Any idea what is really going on here? How can I change the script? Thanks in advance.

Original source