Running str='a "b c" d'
, the quotes are taken literal and have no special meaning afterwards, they are just a character like any other and do not prevent word splitting anymore.
While when assigning the array using quotes, the quotes are evaluated from your shell before the assignment to prevent word splitting:
array1=(a "b c" d);
Btw: Using printf
is a bit easier to showcase the issue then setting up an array and using a loop to echo
the elements:
printf '%s\n' $str
You might use eval
as a workaround, but I would not recommend doing that for any input you cannot 100% control or trust (user input, webscraping stuff, etc.):
eval "printf '%s\n' $str"
#or
eval "array2=( $str )"
Anyways, from your example, I see no reason to use an intermediate variable, just use arrays directly.
$str
- even if"$str"
you just give printable chars like\"b c\"
and not actually quoted string"b c"
(only eval would execute quotes) – alecxs Jun 02 '20 at 12:47array=(a "b c" d)
? – ilkkachu Jun 02 '20 at 12:55str='a b[[:space:]]c d'
should work too (bash) or can be converted automatically (before passing into$str
) likestring="b c"; string="${string//[[:space:]]/[[:space:]]}"; str="a $string d";
(will mask all space, tab, newline, linefeed, formfeed, vertical tab in$string
) – alecxs Jun 02 '20 at 13:24