ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何在bash中使用{}范围表达式

2021-10-10 09:04:33  阅读:211  来源: 互联网

标签:20 .. 16 localhost bash 大括号 root 表达式 范围


导读在编写 shell 脚本时,有时需要生成数字或字符串序列。这种序列数据的一种常见用途是用于循环迭代。

虽然可以使用 seq 之类的专用工具来生成一系列数字,但 bash 本身提供大括号扩展,实际上没有必要在 bash 脚本中添加此类外部依赖项。在本教程中,让我们了解如何使用大括号扩展在 shell 脚本中生成数据序列和一些有用的大括号扩展示例。

{}花括号使用说明

Bash 内置的 range 函数是通过所谓的{}大括号扩展实现的。简而言之,大括号扩展允许根据提供的字符串和数字数据生成字符串序列。大括号扩展的语法如下。

{<string1>,<string2>,...,<stringN>}
{<start-number>..<end-number>}
{<start-number>..<end-number>..<increment>}
<prefix-string>{......}
{......}<suffix-string>
<prefix-string>{......}<suffix-string>

实例一:列出字符串序列

大括号扩展的第一个用例是一个简单的字符串列表,它是大括号内以逗号分隔的字符串列表。这里是简单地列出预定义的字符串数据。

下面使用for循环,列出大括号中的字符串,如下所示。

[root@localhost ~]# for fruit in {apple,orange,lemon}; do echo $fruit ; done
apple
orange
lemon

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式


下面实例是同时创建多个子目录:

[root@localhost ~]# mkdir -p /tmp/users/{dan,john,alex,michael,emma}
[root@localhost ~]# ls -l /tmp/users/
total 0
drwxr-xr-x 2 root root 6 Aug  6 16:23 alex
drwxr-xr-x 2 root root 6 Aug  6 16:23 dan
drwxr-xr-x 2 root root 6 Aug  6 16:23 emma
drwxr-xr-x 2 root root 6 Aug  6 16:23 john
drwxr-xr-x 2 root root 6 Aug  6 16:23 michael

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式


下面是创建多个空文件:

[root@localhost ~]# touch /tmp/file{1,2,3,4}.log
[root@localhost ~]# ll /tmp/
total 0
-rw-r--r-- 1 root root  0 Aug  6 16:24 file1.log
-rw-r--r-- 1 root root  0 Aug  6 16:24 file2.log
-rw-r--r-- 1 root root  0 Aug  6 16:24 file3.log
-rw-r--r-- 1 root root  0 Aug  6 16:24 file4.log

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例二:定义一个数字范围

大括号扩展最常见的用例是为循环迭代定义一个数字范围。你可以使用以下表达式,在其中指定范围的开始/结束,以及可选的增量值。

{<start-number>..<end-number>}
{<start-number>..<end-number>..<increment>}

要定义 10 到 20 之间的整数序列:

[root@localhost ~]# echo {10..20}
10 11 12 13 14 15 16 17 18 19 20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式


可以在循环中使用:

[root@localhost ~]# for num in {10..20}; do echo $num ;done
10
11
12
13
14
15
16
17
18
19
20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式


如果要生成 0 到 20 之间增量为 2 的数字序列:

[root@localhost ~]# echo {0..20..2}
0 2 4 6 8 10 12 14 16 18 20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式


也可以生成一系列递减数字:

[root@localhost ~]# echo {20..10}
20 19 18 17 16 15 14 13 12 11 10

[root@localhost ~]# echo {20..10..-2}
20 18 16 14 12 10

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式


您还可以使用前导零填充数字,以防需要使用相同数量的数字。例如:

[root@localhost ~]# for num in {00..20..2}; do echo $num ; done
00
02
04
06
08
10
12
14
16
18
20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例三:生成字母序列

大括号扩展不仅可用于生成数字序列,还可用于生成字母序列:

[root@localhost ~]# cat brace.sh 
#!/bin/bash
for char1 in {A..B}; do
    for char2 in {A..B}; do
        echo "${char1}${char2}"
    done
done

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例四:生成带有前缀、后缀的字符串序列

使用此功能,可以轻松生成按顺序编号的文件名列表:

[root@localhost ~]# for file in img_{00..05}.jpg; do echo $file ; done
img_00.jpg
img_01.jpg
img_02.jpg
img_03.jpg
img_04.jpg
img_05.jpg

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例五:多个{}花括号组合使用

最后,可以组合多个大括号扩展,在这种情况下会生成所有可能组合。

for char1 in {A..Z}; do
    for char2 in {A..Z}; do
        echo "${char1}${char2}"
    done
done

通过组合两个大括号扩展,下面的单个循环可以产生与上面相同的输出。

for str in {A..Z}{A..Z}; do
    echo $str
done

总结

{}允许您在单个命令行中轻松生成一系列任意字符串。大括号扩展不仅对 shell 脚本有用,而且在命令行环境中也很有用。

标签:20,..,16,localhost,bash,大括号,root,表达式,范围
来源: https://blog.csdn.net/Linuxprobe18/article/details/119833869

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有