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.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
Why does using `${dirs[@]}` ensure all array elements are processed in Bash?
Open an interactive chat with Bash
What is the difference between `${dirs[@]}` and `${dirs[*]}` in Bash arrays?
Open an interactive chat with Bash
How can you count the number of elements in a Bash array?