Consider the code below
#! /bin/bash
declare -a input # unnecessary
declare -a bad
declare -a good # unnecessary
input=('alpha 23' 'bravo 79' 'charlie 12')
echo "input is " ${#input[@]} "long"
for x in "${input[@]}"
do
bad=$x
good[ ${#good[@]} ]=$x
echo
echo "added '$x', good is now " ${#good[@]} "long, bad is still " ${#bad[@]} "long"
done
The output is
input is 3 long
added 'alpha 23', good is now 1 long, bad is still 1 long
added 'bravo 79', good is now 2 long, bad is still 1 long
added 'charlie 12', good is now 3 long, bad is still 1 long
According to the man-page for bash ... "When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero."
I clearly don't understand the part in bold because I expected the statement "bad=$x" to auto-increment the index every time it is executed. It doesn't and is assigning to bad[0] every time.
Why isn't it doing what I expected and is there a better way of writing the code than the clumsy line where I assign to good[ .. ]