linux脚本 花括号,Bash Shell当中的花括号扩展(Brace expansion)

{  } 并没有什么实际的含义,但是却可以作为Brace expansion(大括号扩展或叫做花括号扩展)而经常用于产生各种组个。以下是翻译自 GNU/BASH  man page  的内容:

Brace expansion(大括号扩展或叫做花括号扩展) 是用来随机产生字符串组合的机制。这种机制类似于文件的扩展名,但是并不需要存在响应的文件。Brace expansion(大括号扩展或叫做花括号扩展)模式是一可选的preamble(前导字符),后面跟着一系列逗号分隔的字符串,包含在一对花括号中, 再后面是一个可选的postscript(打印编程语言)。preamble(前导字符)被添加到花括号中的每个字符串前面,postscript(打印编程语言)被附加到每个结果字符串之后, 从左到右进行扩展。  花括号扩展可以嵌套。扩展字符串的结果没有排序;而是保留了从左到右的顺序。

两种格式:

花括号扩展的使用大体上分为两种:

第一种格式为:preamble+{string1,string2,...,stringN}+postscript

左右的花括号是必须的,中间的字符串列表分别由逗号隔开,注意逗号前后不能有空格,如果string中有空格,则需要用单引号或者双引号括起来。

bash在实际扩展时,会将preamble和花括号种的所有字符串(按照从左到右的顺序)相连,最后分别加上postscript。

此外,花括号中间至少有一个逗号,否则bash不会认为这是括号扩展,例如echo {money}就只会输出{money},想要输出money,需要改为echo {money,},如下:# echo {}

{}

# echo {1}

{1}

# echo abc{1}.txt

abc{1}.txt

# echo abc{,1}.txt

abc.txt abc1.txt

# echo abc{1,}.txt

abc1.txt abc.txt

# echo abc{1,2,3}.txt

abc1.txt abc2.txt abc3.txt

# echo abc{1,2,3,""}.txt

abc1.txt abc2.txt abc3.txt abc.txt

# echo abc{1,2,3," "}.txt

abc1.txt abc2.txt abc3.txt abc .txt

# echo abc{1,2,3," 4"}.txt

abc1.txt abc2.txt abc3.txt abc 4.txt

# echo abc{1,2,3,' 4'}.txt

abc1.txt abc2.txt abc3.txt abc 4.txt

注意观察对空格的处理哦!

第二类格式为:preamble+{..[..]}+postscript

其中..组合而成的表达式术语叫做序列表达式(sequence expression),表示一个特定的范围。当和是数字时,代表的是数字范围;当和是单个字母时,代表的是字符范围(默认LC_ALL字符排序)。和必须同为数字或者字母,否则bash不认为是花括号扩展,而是原样输出。来看几个常用的例子:# echo {0..12}

0 1 2 3 4 5 6 7 8 9 10 11 12

# echo {3..-2}

3 2 1 0 -1 -2

# echo {a..g}

a b c d e f g

# echo {g..a}

g f e d c b a

其中的是可选的,代表的是区间范围的递增数,它必须是数字。例如:# echo {0..10..2}

0 2 4 6 8 10

# echo {0..10..-2}

0 2 4 6 8 10

# echo {0..-10..-2}

0 -2 -4 -6 -8 -10

# echo {0..-10..2}

0 -2 -4 -6 -8 -10

从0开始,每递增2个数字就取出相应数字。

如果不指定,那么默认是1或者是-1,具体是1还是-1,要看前面区间范围是递增的还是递减的,比如上面例子的{a..g}中默认为1,{g..a}中默认为-1。

另外,当和是数字时,我们可以通过在数字前面加0来使输出结果的长度保持一致,例如:#  echo {1..10}

1 2 3 4 5 6 7 8 9 10

#  echo {01..10}

01 02 03 04 05 06 07 08 09 10

#  echo {001..10}

001 002 003 004 005 006 007 008 009 010

特殊用法:

组合使用:#  echo {a..z}{0..9}

a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 g0 g1 g2 g3 g4 g5 g6 g7 g8 g9 h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 i0 i1 i2 i3 i4 i5 i6 i7 i8 i9 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9 k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 l0 l1 l2 l3 l4 l5 l6 l7 l8 l9 m0 m1 m2 m3 m4 m5 m6 m7 m8 m9 n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 o0 o1 o2 o3 o4 o5 o6 o7 o8 o9 p0 p1 p2 p3 p4 p5 p6 p7 p8 p9 q0 q1 q2 q3 q4 q5 q6 q7 q8 q9 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 s0 s1 s2 s3 s4 s5 s6 s7 s8 s9 t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 w0 w1 w2 w3 w4 w5 w6 w7 w8 w9 x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 y0 y1 y2 y3 y4 y5 y6 y7 y8 y9 z0 z1 z2 z3 z4 z5 z6 z7 z8 z9

嵌套使用:#  echo {{A..Z},{a..z}}

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z

复杂点的:printf "%s\n" img{00{1..9},0{10..99},{100..999}}.png

输出结果太多,不贴了.

其中img{00{1..9},0{10..99},{100..999}}.png先扩展最外层花括号,结果为:

img00{1..9}.png  img0{10..99}.png  img{100..999}.png

然后从左到右,分别扩展各个花括号(即内层花括号)。

强调:

花括号扩展是bash特有的特性,传统的sh是不支持的。我们可以通过使用set +B来关闭花括号扩展功能,相反的,用set -B使能该功能。#  echo file{1,2}

file1 file2

#  set +B

#  echo file{1,2}

file{1,2}

扩展:

搭配cp使用:

示例1:

将file.txt备份成 file.txt.backup#    cp -v file.txt  file.txt.backup

使用Brace expansion:#    cp -v file.txt{,.backup}

解析: file.txt{,.backup} 这个是整体,#   echo file.txt{,.backup}

file.txt  file.txt.backup

cp -v file.txt{,.backup}  ==  cp -v file.txt  file.txt.backup

示例2:#   touch file{1..10}.txt

#   ls

file10.txt  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  file6.txt  file7.txt  file8.txt  file9.txt

#   mkdir newdir

#   cp -vr  file{1..10..2}.txt  newdir/

‘file1.txt’ -> ‘newdir/file1.txt’

‘file3.txt’ -> ‘newdir/file3.txt’

‘file5.txt’ -> ‘newdir/file5.txt’

‘file7.txt’ -> ‘newdir/file7.txt’

‘file9.txt’ -> ‘newdir/file9.txt’

#   ls newdir/

file1.txt  file3.txt  file5.txt  file7.txt  file9.txt

搭配mv使用:#   mv -v  file{,1}.txt

‘file.txt’ -> ‘file1.txt’

搭配touch 使用:

创建file001.txt 到 file100.txt文件#    touch file{001..100}.txt

和一些其他命令的搭配使用就不一一列举了,其实原理都是一样的.

参考文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值