7

In bash echo {{a,b}.{c,d}} expands to {a.c} {a.d} {b.c} {b.d} Though nothing is mentioned for brace expansion in bash manual for the type of input I have made

My question is why the output is not like this a.c a.d b.c b.d

11

In bash, the expansion of {word} is {word} when the word does not contain a range (..) or a comma.

In your case word happens to contain two brace expansions. These are expanded left to right, so you get, first {a.{c,d}} {b.{c,d}}, and then {a.c} {a.d} {b.c} {b.d} which is the final result.

Note that the outside braces are not expanded since they do not contain a range or a comma. You would get a similar result with @{a,b}.{c,d}@.

Had you tried {{a,b},{c,d}} you would have three expansions (the expansion is done in three steps, each expanding one set of braces). First the outer one into {a,b} {c,d} and then the left into a b {c,d}, and finally a b c d, which is the final result.

See also: Why does "cat {foo}" not output foo, but "cat {foo,bar}" does?

  • Thanks it's now clear to me that braces will only expand if they have coma separated strings or range specified between them. However for the expression {{a,b},{c,d}} the output will be only a b c d in bash, as all the braces will expand. It can also be proved by adding a preamble and a postfix, for example {c{a,o}t,d{o,a}g} – Neo_Returns May 19 '18 at 16:36
  • @Neo_Returns I was confused by your comment and edit-suggestion, then I realized I said "three expansions". I have clarified this now I hope. – Kusalananda May 19 '18 at 17:00

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.