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

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 510 06:41 1.txt
-rw-r--r--  1 root    root   0 510 06:41 2.txt
-rw-r--r--  1 root    root   0 510 06:41 3.txt
-rw-r--r--  1 root    root   0 510 06:41 4.txt
-rw-r--r--  1 root    root   0 510 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值