2

I do not understand why these two loops are different. It must have something to do with when the brace expansion is performed and how white space is interpreted

for i in b{e,\ }s ; do echo $i ; done

for i in bes b s ; do echo $i ; done

and consider this too

for i in $(echo b{e,\ }s) ; do echo $i ; done
2

When you do for i in b{e,\ }s ; do echo $i ; done, the brace expansion b{e,\ }s is expanded into two arguments, one is bes and another is b s so the for loop has two values to iterate over. Note that here b s is a single argument.

On the other hand, in for i in bes b s ; do echo $i ; done, you are explicitly mentioning three arguments to iterate over (separated by spaces), namely bes, b and s.

In the third case, the command substitution $(echo b{e,\ }s) will generate three arguments bes, b and s so again the for loop have three values to iterate over just like the second case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.