Big Shield
Big Shield

Reputation: 632

What is the difference between a non-array var and an array var in bash?

I found that a non-array var can be used as an array var. E.g.:

v=11

for i in "${v[@]}"
do
    printf "$i\n"
done

printf "${#v[@]}"

a=(22 33)

for i in "${a[@]}"
do
    printf "$i\n"
done

So can I say that every var is an array var?

Upvotes: 0

Views: 1139

Answers (1)

chepner
chepner

Reputation: 532303

Strictly speaking, an array parameter is a name with the array attribute set. (Said attribute is set using declare -a.) It would be more accurate to say that the array expansion operators treat non-array parameters as if they were arrays parameters with only one index, 0.

Upvotes: 2

Related Questions