In Bash, placing @ or * after an array name expands the entire array. Inside double quotes, ${array[@]} expands to separate words, one for each element, while ${array[*]} collapses all elements into a single word. Unquoted $dirs expands only to the first element because $dirs refers to ${dirs} by default. ${#dirs[@]} returns the count of elements, not the elements themselves.
Therefore, replacing $dirs with "${dirs[@]}" causes the loop to receive each directory path as an individual word, ensuring that every directory is processed regardless of embedded spaces.