Question Details

No question body available.

Tags

string bash split quotes

Answers (2)

July 9, 2026 Score: 4 Rep: 568,750 Quality: Medium Completeness: 50%

Both quoting and word-splitting are affecting the result.

When double-quoted, "${samples[@]}" expands each array element as one word, and each word is double-quoted. Then the word-splitting for operation gets to see that sequence of words.

If you hadn't used variables it would be as if you had did the following, and for sees three words:

for each in "one 1 uno" "two 2 dos" "three 3 tres"; do

When not double-quoted, ${samples[@]} expands, but does not double-quote the words. So it's as if you had did this:

for each in one 1 uno two 2 dos three 3 tres; do

The for operation sees nine words.

July 9, 2026 Score: 3 Rep: 39,926 Quality: Medium Completeness: 50%

Bill has explained how the for processes "${samples[@]}" (double quoted) and ${samples[@]} (no quotes).

In your case you can use both approaches to obtain the desired result:

i=0

for each in "${samples[@]}"; do # do not word split array elements echo "$i -----------------" echo " ${each}" for item in ${each}; do # do word split an individual array element echo " just the first: ${item}" break # break out of loop after we process the first item done ((i++)) done

This generates:

0 ----------------- one 1 uno just the first: one 1 ----------------- two 2 dos just the first: two 2 ----------------- three 3 tres just the first: three