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

简介: 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 ~]#

————————————————
版权声明:本文为CSDN博主「小黑要上天」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z19861216/article/details/131554305

目录
相关文章
|
6月前
|
监控 Shell Linux
Linux Shell高级用法:优化和自动化你的工作流程
Linux Shell是一个非常强大的工具,可以用于自动化任务、处理文本和数据、进行系统管理等。在这篇文章中,我们将介绍一些Linux Shell的高级用法,帮助你更高效地利用Shell完成各种任务。
102 0
|
7月前
|
Unix Linux
完全指南:mv命令用法、示例和注意事项 | Linux文件移动与重命名
完全指南:mv命令用法、示例和注意事项 | Linux文件移动与重命名
887 0
|
1月前
|
缓存 Ubuntu 网络协议
Linux系统编程之文件I/O函数的使用:介绍文件I/O函数的基本概念、用法和实现方式
Linux系统编程之文件I/O函数的使用:介绍文件I/O函数的基本概念、用法和实现方式
22 1
|
1月前
|
Linux
Linux系统编程之exec函数簇的使用:剖析exec函数簇的实现原理、参数解释和用法技巧
Linux系统编程之exec函数簇的使用:剖析exec函数簇的实现原理、参数解释和用法技巧
38 0
|
1月前
|
存储 Linux
Linux系统编程之Linux 信号集编程:信号集的基本概念、用法和实现方式
Linux系统编程之Linux 信号集编程:信号集的基本概念、用法和实现方式
12 0
|
1月前
|
Linux C语言
Linux系统编程之 Linux信号的编程:信号的基本概念、用法和实现方式
Linux系统编程之 Linux信号的编程:信号的基本概念、用法和实现方式
18 0
|
1月前
|
Linux
linux系统中select函数的用法实现
linux系统中select函数的用法实现
18 0
|
6月前
|
NoSQL Linux 编译器
详讲yum包管理器/Vim编辑器/gdb调试器的基础用法【Linux】
详讲yum包管理器/Vim编辑器/gdb调试器的基础用法【Linux】
77 0
|
6月前
|
Java Linux Windows
【Java用法】Java在Linux下获取当前程序路径以及在Windows下获取当前路径对比
【Java用法】Java在Linux下获取当前程序路径以及在Windows下获取当前路径对比
135 0
|
2月前
|
存储 Shell Linux
Linux的shell命令——变量用法
Linux的shell命令——变量用法
33 0