array without an index always expands to the element in the index 0 (saying "the first element" is not accurate)

meleu
2023-03-26 14:03:43 -03:00
parent 7256a763d2
commit 6d579da423

@@ -1,4 +1,4 @@
## Expanding an array without an index only gives the first element.
## Expanding an array without an index only gives the element in the index 0.
### Problematic code:
@@ -22,7 +22,7 @@ done
### Rationale:
When referencing arrays, `$myarray` is equivalent to `${myarray[0]}` -- it results in only the first of multiple elements.
When referencing arrays, `$myarray` is equivalent to `${myarray[0]}` -- which is usually the first of multiple elements. This is also true for associative arrays. Therefore, if 0 (zero) is not a valid key, `$myarray` expands to an empty string.
To get all elements as separate parameters, use the index `@` (and make sure to double quote). In the example, `echo "${myarray[@]}"` is equivalent to `echo "foo" "bar"`.