On Thu, Aug 31, 2023 at 04:13:09PM +0100, Kerin Millar wrote: > This script is a mess because its functions begin by defining RESULT as an > ordinary, non-array variable (if not yet declared). > > $ RESULT='string' > $ declare -p RESULT # not yet an array variable > declare -- RESULT='string' > > It then proceeds to operate on the variable in such a way that it will be > transformed to an array. > > $ RESULT[1]='second element' > $ declare -p RESULT > declare -a RESULT=([0]="string" [1]="second element") > > Now, would RESULT='' empty this array? No, it would not. > > $ RESULT='' # no different from RESULT[0]='' > $ declare -p RESULT > declare -a RESULT=([0]="" [1]="second element") > > A correct way to initialise an empty array variable is RESULT=(). > > $ RESULT=() > $ declare -p RESULT # now an empty array > declare -a RESULT=() > > You might also consider using the "local" builtin to declare the variable > with a function-local scope.
All of this is excellent advice. If you'd like a tutorial that explains more about bash arrays, I recommend: https://mywiki.wooledge.org/BashFAQ/005