bash incrementally adding to an array
<p>Consider the code below</p>

<pre><code>#! /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
</code></pre>

<p>The output is </p>

<pre><code>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
</code></pre>

<p>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; <strong>otherwise  the  index  of  the  element  assigned is the last index assigned to by the <em>statement</em> plus one</strong>.  Indexing starts at zero."</p>

<p>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.</p>

<p>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[ .. ]</p>