I wrote in the terminal:
arr=(1 2 3)
for x in $arr; do
echo $x
done
and it just prints '1'. Why doesn't it print 1 2 3 ?
Change
for x in $arr; do
to
for x in "${arr[@]}"; do
To expand to all the elements of an array, use "${arr[@]}"
for x in "${arr[@]}"; do
When you use the array name as an ordinary variable, without indexing it, it expands to the first element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With