我可以在没有 `eval` 的情况下在 {} 扩展中使用变量吗?

n.r*_*.r. 7 bash wildcards brace-expansion variable-substitution

我可以在{}扩展中使用变量而不吸引eval吗?如果是这样,如何?

这不起作用:

$ touch 1.foo 1.bar
$ ls 1.{foo,bar}
1.bar  1.foo
$ extensions=foo,bar
$ ls 1.{$extensions}
ls: cannot access 1.{foo,bar}: No such file or directory
Run Code Online (Sandbox Code Playgroud)

它适用于eval

$ eval ls 1.{$extensions}
1.bar  1.foo
Run Code Online (Sandbox Code Playgroud)

Gil*_*il' 9

大括号扩展在扩展期间(实际上是第一件事)发生的很早,在变量扩展之前。要对变量扩展的结果执行大括号扩展,您需要使用eval.

如果不使用通配符模式而不是大括号模式,eval则可以实现相同的效果extensions。设置extglob选项以激活类似 ksh 的模式

shopt -s extglob
extensions='@(foo|bar)'
ls 1.$extensions
Run Code Online (Sandbox Code Playgroud)