Bash – Why in bash {{a,b}.{c,d}} expands to {a.c} {a.d} {b.c} {b.d}

bashbrace-expansion

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

Best Answer

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?

Related Question