问题描述
我正在尝试达到与打字相同的效果
I am trying achieve the same effect as typing
mv ./images/*.{pdf,eps,jpg,svg} ./images/junk/
在命令行,从 bash 脚本内部.我有:
at the command line, from inside a bash script. I have:
MYDIR="./images"
OTHERDIR="./images/junk"
SUFFIXES='{pdf,eps,jpg,svg}'
mv "$MYDIR/"*.$SUFFIXES "$OTHERDIR/"
运行时,会出现意外错误:
which, when run, gives the not unexpected error:
mv: rename ./images/*.{pdf,eps,jpg,svg} to ./images/junk/*.{pdf,eps,jpg,svg}:
No such file or directory
引用所有这些的正确方法是什么,以便 mv
实际上会进行所需的扩展?(是的,有很多文件与 ./images/
中的模式匹配.)
What is the correct way to quote all this so that mv
will actually do the desired expansion? (Yes, there are plenty of files that match the pattern in ./images/
.)
推荐答案
已删除的答案是正确的.对您的尝试稍作修改:
A deleted answer was on the right track. A slight modification to your attempt:
shopt -s extglob
MYDIR="./images"
OTHERDIR="./images/junk"
SUFFIXES='@(pdf|eps|jpg|svg)'
mv "$MYDIR/"*.$SUFFIXES "$OTHERDIR/"
大括号扩展在变量扩展之前完成,但变量扩展在路径名扩展之前完成.所以当你原来的变量被扩展时,大括号仍然是大括号,但是当变量包含路径名元素时,当路径名扩展完成时它们已经被扩展了.
Brace expansion is done before variable expansion, but variable expansion is done before pathname expansion. So the braces are still braces when the variable is expanded in your original, but when the variable instead contains pathname elements, they have already been expanded when the pathname expansion gets done.
这篇关于如何让bash扩展变量中的通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论