Use double quotes as part of the string in a Bash array

So I need to create an array that has " in the string of the text:

string = ( "value 1" "value2"

where the actual string is "value1" with the quotations included

would this work?

string = ( \"value1\" \"value\")

and if the strings contain spaces as well:

string = ("\"this is a test\"" "\"this is only a test\"")

Thanks in advance. I think this will work but just trying to verify my logic.

Try using single quotes:

string=('"value1"' '"value2"')

echo ${string[0]}
"value1"

echo ${string[1]}
"value2"

so what about multiple quotes in the string?

I.E.

code = ('"value1" "value2"' '"value3" "value4"')

Thanks again.

---------- Post updated at 12:34 PM ---------- Previous update was at 11:58 AM ----------

#!/usr/bin/bash
string=( '"value1" "value2"'
         '"value3" "value4"' )
for i in ${string[@]}; do
echo $i
done

outputs:

"value1"
"value2"
"value3"
"value4"

the expected and desired output would be:

"value1" "value2" 
"value3" "value4" 

Enclose it in double quotes to preserve the blank spaces:

for i in "${string[@]}"; do
1 Like

strong with the fu you are....