${#array[@]}
to get the 'size' of an array, it actually only returns the number of assigned elements in the array.e.g.
> array[23]=123
> echo ${#array[@]}
1
Hmm... only 1? Not 24?
As far as I can tell, there's no way around this. Just don't expect this behaviour and fill your arrays wisely.
While we're on bash arrays, remember that you can change the 'join' character for naive printing of arrays by manipulating the IFS (Internal Field Separator) variable. Below I also show that the quotation context is important for this:
> array[0]=1;array[1]=2;array[2]=3;
> echo ${array[*]}
1 2 3
> echo "${array[*]}"
1 2 3
> IFS=","
> echo ${array[*]}
1 2 3
> echo "${array[*]}"
1,2,3
>
Note: It's best practice to store and then restore the original IFS variable.
> ORIG_IFS=IFS
... do stuff
> IFS=ORIG_IFS
No comments:
Post a Comment