如何让bash扩展变量中的通配符?

编程入门 行业动态 更新时间:2024-10-27 10:34:41
本文介绍了如何让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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 02:40:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/674932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   变量   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!