0

I want to create an array like so

array=(

"element1 with "quoted string""
"element2 without double quoted string"
"element3"
)

After running the code it outputs

echo ${array[0]}                                                               
element1 with quoted.

I'm trying to include the quotes in the echo'd output. How do I do this?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
pythonic12
  • 41
  • 1
  • 5
  • You should also put double-quotes around the variable (/array) reference when you use it, e.g. `echo "${array[0]}"` instead of just `echo ${array[0]} `. This will not affect double-quotes in the string value, but will avoid trouble with some other shell metacharacters. – Gordon Davisson Jun 19 '19 at 21:23

1 Answers1

2

Either use single quotes,

array=(
'element1 with "quoted string"'
...
)

or escape the literal double quotes:

array=(
"element1 with \"quoted string\""
...
)

Your first nested quote closes the opening quote, but quoted still considered part of the current word. The next element of the array is string with an empty string appended to its end.

chepner
  • 497,756
  • 71
  • 530
  • 681