如何使bash扩展变量中的通配符? [英] how to make bash expand wildcards in variables?

查看:64
本文介绍了如何使bash扩展变量中的通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试达到与打字相同的效果

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆