Linux Shell脚本编程-花括号

花括号可以定义一个集合,集合内可以定义多个可以枚举的项,每个项使用逗号进行分割。主要是起到将集合内的项进行展开,将每一个项与外面的字符串进行组合。例如:

cp test1.conf{,.bak}

上面的写法与下面的写法是一样的:

cp test1.conf test1.conf.bak

如果一个命令支持多个参数,那么他就可以通过花括号来实现,例如:

mkdir app{1,2} # mkdir app1 app2

通过花括号可以定义一个序列:

echo test{0..10}  # test0 test1 test2 test3 test4 test5 test6 test7 test8 test9 test10
echo test-{a..z} # test-a test-b test-c test-d test-e test-f test-g test-h test-i test-j test-k test-l test-m test-n test-o test-p test-q test-r test-s test-t test-u test-v test-w test-x test-y test-z

echo {001..10} # 001 002 003 004 005 006 007 008 009 010
# 步长
echo {001..10..2} # 001 003 005 007 009

# 组合使用,相当于笛卡尔积
echo test{a..f}-{0..3} # testa-0 testa-1 testa-2 testa-3 testb-0 testb-1 testb-2 testb-3 testc-0 testc-1 testc-2 testc-3 testd-0 testd-1 testd-2 testd-3 teste-0 teste-1 teste-2 teste-3 testf-0 testf-1 testf-2 testf-3

你可能感兴趣的:(Linux,linux,运维,服务器)