Search code examples
arraysbashis-emptyquoting

traverse bash array including empty elements


array=('' '' 2 4); for f in ${array[@]}; do echo a; done
a
a

I expected four "a" rows here as there are 4 elements of array

$ echo ${#array[@]}

4

Solution

  • You must quote the variable for correct using:

    array=('' '' 2 4); for f in "${array[@]}"; do echo a; done
    

    and btw. search for simular answers :)