linux下${}、$()、$[]、$(())、[]、[[]]、(())的作用及用法说明

在linux下,特别是shell脚本中,我们经常会遇到${}、$()、$[]、$(())、[]、[[]]、(()),眼花之凌乱,让我们傻傻分不清,下面就为大家讲解一下它们的作用及主要用法

1.${}

首先,当${}用来引用变量时,其等价于$,只不过${}可以指定变量边界

[root@rhel77 yum]# a=123
[root@rhel77 yum]# b=$a
[root@rhel77 yum]# echo $b
123
[root@rhel77 yum]# b=${a}
[root@rhel77 yum]# echo $b
123
[root@rhel77 yum]# 

其次,${}可以用来对字符串进行截取及替换处理

字符串截取及替换处理
变量设定方式说明
${string#substring}从$string的开头位置截掉最短匹配的$substring
${string##substring}从$string的开头位置截掉最长匹配的$substring
${string%substring}从$string的结尾位置截掉最短匹配的$substring
${string%%substring}从$string的结尾位置截掉最长匹配的$substring
${string/substring/replacement}使用$replacement来替换第一个匹配的$substring
${string//substring/replacement}使用$replacement来替换所有匹配的$substring
${string/#substring/replacement}如果$substring匹配$string的开头部分, 那么就用$replacement来替换$substring
${string/%substring/replacement}如果$substring匹配$string的结尾部分, 那么就用$replacement来替换$substring

E.g:

${string#substring}         –>从$string的开头位置截掉最短匹配的$substring

[root@rhel77 ~]# test=xyzXYZ123456xyzXYZ
[root@rhel77 ~]# echo ${test#x*Z}
123456xyzXYZ
[root@rhel77 ~]# 

${string#substring}         –>从$string的开头位置截掉最长匹配的$substring

[root@rhel77 ~]# test=xyzXYZ123456xyzXYZ
[root@rhel77 ~]# echo ${test##x*Z}

[root@rhel77 ~]# 

${string%substring}        –>从$string的结尾位置截掉最短匹配的$substring

[root@rhel77 ~]# test=xyzXYZ123456xyzXYZ
[root@rhel77 ~]# echo ${test%x*Z}
xyzXYZ123456
[root@rhel77 ~]# 

${string%%substring}        –>从$string的结尾位置截掉最长匹配的$substring

[root@rhel77 ~]# test=xyzXYZ123456xyzXYZ
[root@rhel77 ~]# echo ${test%%x*Z}

[root@rhel77 ~]# 

${string/substring/replacement}        –>使用$replacement来替换第一个匹配的$substring

[root@rhel77 ~]# test=xyzXYZ123456xyzXYZ
[root@rhel77 ~]# echo ${test/xyz/ztj}
ztjXYZ123456xyzXYZ
[root@rhel77 ~]# 

${string//substring/replacement}        –>使用$replacement来替换所有匹配的$substring

[root@rhel77 ~]# test=xyzXYZ123456xyzXYZ
[root@rhel77 ~]# echo ${test//xyz/ztj}
ztjXYZ123456ztjXYZ
[root@rhel77 ~]# 

${string/#substring/replacement}        –>如果$substring匹配$string的开头部分, 那么就用$replacement来替换$substring

[root@rhel77 ~]# echo ${test/#xyz/ztj}
ztjXYZ123456xyzXYZ
[root@rhel77 ~]# 

${string/%substring/replacement}        –>如果$substring匹配$string的结尾部分, 那么就用$replacement来替换$substring

[root@rhel77 ~]# test=xyzXYZ123456XYZxyz
[root@rhel77 ~]# echo ${test/%xyz/ztj}
xyzXYZ123456XYZztj
[root@rhel77 ~]# 注意上下对比
[root@rhel77 ~]# test=xyzXYZ123456xyzXYZ
[root@rhel77 ~]# echo ${test/%xyz/ztj}
xyzXYZ123456xyzXYZ
[root@rhel77 ~]# 

第三,${}也可以对变量进行判断赋值–了解即可,很少使用

变量赋值判断
变量设定方式说明
${parameter-default}如果变量parameter没被声明, 那么就使用默认值
${parameter:-default}如果变量parameter没被设置, 那么就使用默认值
${parameter+alt_value}如果变量parameter被声明了, 那么就使用alt_value, 否则就使用null字符串
${parameter:+alt_value}如果变量parameter被设置了, 那么就使用alt_value, 否则就使用null字符串
${parameter=default}如果变量parameter没声明, 那么就把它的值设为default
${parameter:=default}如果变量parameter没设置, 那么就把它的值设为default
${parameter?err_msg}如果parameter已经被声明, 那么就使用设置的值, 否则打印err_msg错误消息
${parameter:?err_msg}如果parameter已经被设置, 那么就使用设置的值, 否则打印err_msg错误消息

对于${parameter-default}和${parameter:-default}、${parameter+alt_value}和${parameter:+alt_value}、${parameter=default}和${parameter:=default}、${parameter?err_msg}和${parameter:?err_msg},99.99%情况下,${parameter-default}和${parameter:-default}、${parameter+alt_value}和${parameter:+alt_value}、${parameter=default}和${parameter:=default}、${parameter?err_msg}和${parameter:?err_msg}绝大多数情况下,输出都是一样的。只有在parameter被声明并设置为null值的时候, 多出来的:才会引起这两种形式的不同。

最后,${}也可以获取变量长度

定义数组:

ztj=(ab cd ef ghij123)

[root@rhel77 ~]# ztj=(ab cd ef ghij123)    -定义数组
[root@rhel77 ~]# echo ${ztj[@]}            -输出数组全部元素
ab cd ef ghij123
[root@rhel77 ~]# echo ${ztj[*]}            -输出数组全部元素
ab cd ef ghij123
[root@rhel77 ~]# 
[root@rhel77 ~]# echo ${ztj[0]}            -输出数组第一个元素,默认数组下标从0开始
ab
[root@rhel77 ~]# echo ${#ztj[@]}           -统计数组元素的个数
4
[root@rhel77 ~]# echo ${#ztj[*]}           -统计数组元素的个数
4
[root@rhel77 ~]# echo ${#ztj[3]}           -计算数组第四个元素的长度,即:ghij123的长度
7
[root@rhel77 ~]# ztj[0]=ztj                -将数组第一个元素的数值重新赋值为ztj
[root@rhel77 ~]# echo ${ztj[@]}            -输出数组全部元素进行验证
ztj cd ef ghij123
[root@rhel77 ~]# 

2.$()

在linux中,$()是用来做命令替换的,其等价于“(反引号)。但是在shell脚本,我们一般使用$(),以增加脚本的可读性

[root@rhel77 ~]# echo $(date +%Y%m%d)
20230706
[root@rhel77 ~]# echo `date +%Y%m%d`
20230706
[root@rhel77 ~]# 

例外:

1.“(反引号)不能多次嵌套使用,否则会有异常问题

E.g:

[root@rhel77 ~]# echo ztj `echo test ` echo date is `date +%Y%m%d```
ztj test echo date is 20230706      ---echo date异常输出
[root@rhel77 ~]# 

2.$()可以多次嵌套使用,而不会有问题

[root@rhel77 ~]# echo ztj $(echo test $( echo date is $(date +%Y%m%d)))
ztj test date is 20230706             ---echo date正常输出
[root@rhel77 ~]# 

3.“(反引号)和$()虽然可以混合使用,但是可读性较差,不建议使用

[root@rhel77 ~]# echo ztj `echo test $( echo date is $(date +%Y%m%d))`
ztj test date is 20230706
[root@rhel77 ~]# 

3.$[]

在linux中,$[]是用来进行数学运算的,其等价于$(()),支持+-*/%,并且在使用中变量可以不使用$,直接使用变量名即可

[root@rhel77 ~]# echo $[1+2]
3
[root@rhel77 ~]# a=1
[root@rhel77 ~]# b=2
[root@rhel77 ~]# echo $[a+b]
3
[root@rhel77 ~]# echo $[$a+$b]
3
[root@rhel77 ~]# 

4.$(())

在linux中,$(())是用来进行数学运算的,其等价于$[],支持+-*/%,并且在使用中变量可以不使用$,直接使用变量名即可

[root@rhel77 ~]# echo $((1+2))
3
[root@rhel77 ~]# a=1
[root@rhel77 ~]# b=2
[root@rhel77 ~]# echo $((a+b))
3
[root@rhel77 ~]# echo $(($a+$b))
3
[root@rhel77 ~]# 

5.[]

在linux中,[]用于判断表达式的是0或非0,以决定程序的执行顺序,其等价于test命令(几乎不常用)

–>逻辑判断

[root@rhel77 ~]# [ ! -d ztj ] && mkdir ztj     --目录ztj不存在,则进行创建
[root@rhel77 ~]# ls -ld ztj
drwxr-xr-x 2 root root 6 Jul  6 10:04 ztj
[root@rhel77 ~]# 

–>if命令判断

[root@rhel77 ~]# {
> a=java
> if test $a == "linux"
> then
> echo "i am linux"
> elif [ $a == "java" ] 
> then
> echo "i am java"
> fi
> }
i am java
[root@rhel77 ~]# {
> a=linux
> if test $a == "linux"
> then
> echo "i am linux"
> elif [ $a == "java" ] 
> then
> echo "i am java"
> fi
> }
i am linux
[root@rhel77 ~]# 

6.[[]]

在linux中,[[]]是[]的增强版,其返回值也是0或者非0,并且,在[[ ]]中使用> 、< 等符号不需要转义

[root@rhel77 ~]# {
> a=4
> if [ $a \> 5 ]
> then
>         echo "$a的值大于5"
> else
>         echo "$a的值小于5"
> fi
> }
4的值小于5
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> a=4
> if [[ $a > 5 ]]
> then
>         echo "$a的值大于5"
> else
>         echo "$a的值小于5"
> fi
> }
4的值小于5
[root@rhel77 ~]# 

另外:

–>[[]]支持&&和||,也支持==和!=和=~的连接或组合判断(但是不支持>、<、>=、<=等)

[root@rhel77 ~]# {
> a=1
> if [[ $a != 4 && $a != 5 ]] 
> then
> echo "hello i am ztj"
> fi
> }
hello i am ztj
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> a=1
> if [[ $a != 4 ]] && [[ $a != 5 ]] 
> then
> echo "hello i am ztj"
> fi
> }
hello i am ztj
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> a=1
> if [ $a != 4 -a  $a != 5 ]
> then
> echo "hello i am ztj"
> fi
> }
hello i am ztj
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> arch_version=$(arch)
> os_version=$(cat /etc/redhat-release)
> echo $os_version
> 
> if [[ "$arch_version" == "x86_64" ]] &&  [[ "$os_version" =~ "Red Hat Enterprise Linux Server release 7.7 (Maipo)" ]]
> then
>      echo "OS is ok"
> fi
> }
Red Hat Enterprise Linux Server release 7.7 (Maipo)
OS is ok
[root@rhel77 ~]# 

–>[[ ]]在比较字符串支持正则匹配和通配符匹配,比如:在[[ ]]中进行 == 或者 != 或=~比较时可以进行通配符匹配

[root@rhel77 ~]# {
> a=linux
> if [[ $a == li* ]]
> then
> echo "hello i am ztj"
> fi
> }
hello i am ztj
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> arch_version=$(arch)
> os_version=$(cat /etc/redhat-release)
> echo $os_version
> 
> if [[ "$arch_version" == "x86_64" ]] &&  [[ "$os_version" =~ ^"Red Hat Enterprise Linux Server release 7".* ]]
> then
>      echo "OS is ok"
> fi
> }
Red Hat Enterprise Linux Server release 7.7 (Maipo)
OS is ok
[root@rhel77 ~]# 

7.(())

在linux中,(())除了结合$进行数学运算之外,还可以用于for或while循环命令中控制循环,类似于c语言,当改变变量的值时,且不需要使用$

[root@rhel77 ~]# {
> for((i=1;i<5;i++))
> do
>         echo "this is $i"
> done
> }
this is 1
this is 2
this is 3
this is 4
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> i=0
> while [ $i -le 5 ]
> do
>         echo "this is $i"
>         ((i++))
> done
> }
this is 0
this is 1
this is 2
this is 3
this is 4
this is 5
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> i=0
> while [ $i -le 5 ]
> do
>         echo "this is $i"
>         ((i=i+1))
> done
> }
this is 0
this is 1
this is 2
this is 3
this is 4
this is 5
[root@rhel77 ~]# 
[root@rhel77 ~]# {
> i=0
> while [ $i -le 5 ]
> do
>         echo "this is $i"
>         ((++i))
> done
> }
this is 0
this is 1
this is 2
this is 3
this is 4
this is 5
[root@rhel77 ~]# 

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年12月7日
下一篇 2023年12月7日

相关推荐