1

I am trying to construct the following command from a bash script:

>> mycommand -opt "third_party_app, 'app_option_a', 1"

However, when I constructed the command in a bash script like the following:

app_option="app_option_a"
app_option_val=1
tmp1='"third_party_app, '
tmp2="'${app_option}', ${app_option_val}"
tmp3='"'
optval="$tmp1$tmp2$tmp3"
echo $optval # to show that $optval looks right
command=(mycommand -opt $optval)
"${command[@]}"

I got the following command line output:

>> "third_party_app, 'app_option_a', 1"
>> + mycommand -opt '"third_party_app,' ''\''app_option_a'\'',' '1"'

I kind of get that why a pair of single quote is put around the content of $optval in the prompt output, but I am completely thrown off by all the added single quotes and back slashes in $optval's content. I know I am missing some fundamentals, so please point me in the right direction.

I have read this post and this post, they are helpful but don't give a solution to how a command argument can be constructed within a pair of double quotes, like command "argument that needs to be in double quotes". Instead, their answers only lead to command 'argument that needs to be in double quotes'.

DXZ
  • 451
  • 4
  • 14
  • Quote `$optval`: `command=(mycommand -opt "$optval")` – Wiimm Aug 10 '20 at 06:04
  • @DXZ: You don' pass any double quotes to _mycommand_ in your example. Note that "foo'" evaluates fo `foo'`. – user1934428 Aug 10 '20 at 08:18
  • @Wiimm, adding double-quotes around $optval in the `command` array doesn't make a difference. – DXZ Aug 10 '20 at 15:20
  • @user1934428, can you elaborate on how that is leading to the inserted single quotes and backslashes in the example? – DXZ Aug 10 '20 at 15:23
  • @DXZ : In your example, you explicitly inserted these characters, for instance when doing the `tmp3='"'`. I don't know why you want to have quotes in your arguments. Commands which want to have them are rare. One example where you want them is when you are constructing JSON code. – user1934428 Aug 11 '20 at 06:03
  • @user1934428, that is out of my control. Although I say 'mycommand', it is actually a company tool that I am invoking and the format of passing that particular argument has to have the double-quotes. – DXZ Aug 12 '20 at 13:21

0 Answers0