From 6d579da42334cb497d61e91a780063917954aaf3 Mon Sep 17 00:00:00 2001 From: meleu Date: Sun, 26 Mar 2023 14:03:43 -0300 Subject: [PATCH] array without an index always expands to the element in the index 0 (saying "the first element" is not accurate) --- SC2128.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SC2128.md b/SC2128.md index e68e258..ea753ac 100644 --- a/SC2128.md +++ b/SC2128.md @@ -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"`.