I am trying to append an item to an array that is stored in a variable, however it's not acting entirely how I'd expect it to.
Here is what I'm trying to do:
array=()
item_to_add="1 '2 3'"
array+=(${item_to_add})
for item in "${array[@]}"; do
echo "item: ${item}"
done
I'd expect this to output the following:
item: 1
item: '2 3'
However I get the following output instead:
item: 1
item: '2
item: 3'
Is there any way to make it act like this code without using something like eval
?
array=()
array+=(1 '2 3')
for item in "${array[@]}"; do
echo "item: ${item}"
done
And the output from it:
item: 1
item: '2 3'
xargs
parses quotes in its input. This is usually a (specification-level) bug rather than a feature (it makes filenames with literal quotations nearly impossible to work with without non-POSIX extensions such as-d
or-0
overriding the behavior), but in present circumstances it comes in quite handy:...emits...