admin管理员组文章数量:1301582
Using bash shell: Instead of ls *.{html,txt,pdf}
(which results in e.g. a.txt b.pdf c.html
) I would like to use something more adaptable like exts="html,txt,pdf" ; ls *.{$exts}
. But the output of this is ls: cannot access '*.{html,pdf,txt}': No such file or directory
.
How can I get the correct result with a variabe as list of extensions?
Using bash shell: Instead of ls *.{html,txt,pdf}
(which results in e.g. a.txt b.pdf c.html
) I would like to use something more adaptable like exts="html,txt,pdf" ; ls *.{$exts}
. But the output of this is ls: cannot access '*.{html,pdf,txt}': No such file or directory
.
How can I get the correct result with a variabe as list of extensions?
2 Answers
Reset to default 5One option is to use the "extended globbing" feature supported by Bash. (See the extglob section in glob - Greg's Wiki.)
shopt -s extglob
exts='html|txt|pdf'
ls *.@($exts)
Use find
:
exts=html,pdf,txt; find . -regextype posix-extended -regex ".*\.(${exts/,/|})$" -printf '%f\n'
本文标签: bashVariable inside bracesStack Overflow
版权声明:本文标题:bash - Variable inside braces - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741661718a2391078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
eval
uate it, which is kind of dangerous – Fravadona Commented Feb 11 at 13:51