mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
array without an index always expands to the element in the index 0 (saying "the first element" is not accurate)
@@ -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:
|
### Problematic code:
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ done
|
|||||||
|
|
||||||
### Rationale:
|
### 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"`.
|
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"`.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user