0

I have a variable containing comma-separated list, and I need to use it for brace expansion"

foo=aa,bb,cc

echo $foo

echo /tmp/{$foo}

but that just prints literally /tmp/{aa,bb,cc}.

Whereas, when I do it without variable, it works:

echo /tmp/{xx,yy,zz}
/tmp/xx /tmp/yy /tmp/zz

why does the firs case not work?

braX
  • 11,506
  • 5
  • 20
  • 33
400 the Cat
  • 266
  • 3
  • 23
  • 2
    Are you sure you're using dash? It doesn't support brace expansion, AFAIK. Anyway, quoting the bash man: `The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.`. As you can see, brace expansion is done before variable expansion, so contents of the variable can't influence the brace expansion. It's just how bash works, you'll have to find another way. Something like `echo $(IFS=,; printf '/tmp/%s ' $foo)` might work. – Discussian Mar 31 '23 at 06:46
  • See [here](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06) for a list of expansions supported by POSIX. As you see, brace expansion is not among them. In your "working" (expanding) example, you use a shell which does not exactly comply to POSIX. A POSIX compatible shell should output `/tmp/{aa,bb,cc}` in both cases. – user1934428 Mar 31 '23 at 07:12
  • 1
    BTW, I just retestet your case with `dash` and `echo /tmp/{xx,yy,zz}` **does** output _/tmp/{xx,yy,zz}_ . – user1934428 Mar 31 '23 at 07:14
  • Even if you are using a shell that supports brace expansions, I don't know of any that will accept the expansion of `{$foo}` as the expression `{aa,bb,cc}` to expand. `bash` simply ignore `{$foo}` as an invalid brace expansion, and `zsh`, while allowing parameter expansion to produce the *elements* of a brace expansion (e.g., `x=aa; y=bb; z=cc; echo /tmp/{$x,$x,$z}`), won't allow the commas themselves to be supplied by the result of a parameter expansion. – chepner Apr 02 '23 at 22:27

0 Answers0