irpas技术客

Linux之shell中的大括号、中括号、小括号的使用详解_keep hungry always_linux 各种括号的使用

未知 3312

Linux之shell中的大括号、中括号、小括号的使用详解及示例

**摘要:**很多人和我一样对于shell的各种括号的各种用法肯定不是很清楚,有时候看见别人脚本都不知道是什么意思,今天就来说说bash中的大中小括号的用法和解释,本人常用bash所以也只能用bash来说明了,若其他shell有出入请勿怪我。

一、shell中的大括号 "{}"的用法: 1、常用方法 [root@cn21 ~]# var=login [root@cn21 ~]# echo "aa$var" aalogin [root@cn21 ~]# echo "$varaa" [root@cn21 ~]# echo "$var aa" login aa [root@cn21 ~]# echo "${var}aa" #注意此时大括号作用 loginaa

解释:当变量名和后面的内容都是变量命名所允许的内容时候这时候直接用$var是不行的得用{}把变量名括起来

2、{1,2,3}和{1…n} [root@cn21 test]# touch {1..5}.txt [root@cn21 test]# ll 总用量 52 -rw-r--r-- 1 root root 0 5月 10 06:41 1.txt -rw-r--r-- 1 root root 0 5月 10 06:41 2.txt -rw-r--r-- 1 root root 0 5月 10 06:41 3.txt -rw-r--r-- 1 root root 0 5月 10 06:41 4.txt -rw-r--r-- 1 root root 0 5月 10 06:41 5.txt [root@cn21 test]# touch {4,5,6,7,8}.tx [root@cn21 test]# ls |grep tx$ 4.tx 5.tx 6.tx 7.tx 8.tx 二、shell中的中括号"[ ]"的用法: 1、单中括号 []

①bash 的内部命令,[和test是等同的。如果我们不用绝对路径指明,通常我们用的都是bash自带的命令。if/test结构中的左中括号是调用test的命令标识,右中括号是关闭条件判断的。这个命令把它的参数作为比较表达式或者作为文件测试,并且根据比较的结果来返回一个退出状态码。if/test结构中并不是必须右中括号,但是新版的Bash中要求必须这样。

[root@cn21 test]# if test -d /test;then echo "hello" > fi hello [root@cn21 test]# if [ -d /test ];then echo "hello" fi hello #test和[]中可用的比较运算符只有==和!=,两者都是用于字符串比较的,不可用于整数比较 #整数比较只能使用-eq,-gt这种形式 #无论是字符串比较还是整数比较都不支持大于号小于号。 [root@cn21 test]# cat /etc/passwd|grep -E '^[rotgm]' root:x:0:0:root:/root:/bin/bash mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin #字符范围。用作正则表达式的一部分,描述一个匹配的字符范围。作为test用途的中括号内不能使用正则。 [root@cn21 test]# array1=(zhangsan lisi wang5 maliu) [root@cn21 test]# echo ${array1[2]} #在一个array 结构的上下文中,中括号用来引用数组中每个元素的编号 wang5 2、双中括号[[ ]]

双方括号里面的表达式使用了test命令中采用的标准字符串比较。但它提供了test命令未提供的另一个特性——模式匹配。

[root@cn21 test]# user=test [root@cn21 test]# if [[ $user == t* ]];then echo "hello"; fi hello [root@cn21 test]# # [[是 bash 程序语言的关键字。并不是一个命令,[[ ]] 结构比[ ]结构更加通用。在[[和]]之间所有的字符都不会发生文件名扩展或者单词分割,但是会发生参数扩展和命令替换。 # 支持字符串的模式匹配,使用=~操作符时甚至支持shell的正则表达式。字符串比较时可以把右边的作为一个模式,而不仅仅是一个字符串,比如[[ hello == hell? ]], 结果为真。[[ ]] 中匹配字符串或通配符,不需要引号。 # 使用[[ ... ]]条件判断结构,而不是[ ... ],能够防止脚本中的许多逻辑错误。比如,&&、||、<和> 操作符能够正常存在于[[ ]]条件判断结构中,但是如果出现在[ ]结构中的话,会报错。 # bash把双中括号中的表达式看作一个单独的元素,并返回一个退出状态码。
三、shell中的小括号 "( )"的用法: 1、() 数组赋值 [root@cn21 test]# array1=(zhangsan lisi wang5 maliu) [root@cn21 test]# echo ${array1[2]} #在一个array 结构的上下文中,中括号用来引用数组中每个元素的编号 wang5 2、() 子shell赋值 [root@cn21 test]# var=12345 [root@cn21 test]# (var=local;echo $var) local [root@cn21 test]# echo $var 12345

在子shell中变量var值为local,但是在上级shell中就不是这个值,可以看出是在子shell中有效的赋值

3、() 命令集合结果重定向 [root@cn21 test]# (echo "a";echo "b")|awk '{print NR,$0}' 1 a 2 b 4、$()用法 [root@cn21 test]# echo $(cat /etc/hosts) #引用命令 dd d d ddfafaffaf drrrrrtttt deeeeeeef ddddddddd dddddde 5、(())用法 [root@cn21 test]# echo $((2+3)) 5 [root@cn21 test]# echo $((2*3)) 6 [root@cn21 test]# echo $((2/3)) 0 [root@cn21 test]# echo $((4/3)) 1 [root@cn21 test]# echo $((4%3)) 1 [root@cn21 test]# echo $((25>4 ? 2:3)) 2 [root@cn21 test]# echo $((25>4 ? 5:3)) 5


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Linux #各种括号的使用 #一shell中的大括号