shell 字符串操作 + 变量替换

本篇内容包括:

字符串操作
变量替换
1.字符串操作

表达式 含义

1.获取长度:

zhang@zhang-virtual-machine:~/Desktop$ string='my blog nickName is xiao mu xi zi'
zhang@zhang-virtual-machine:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${#string}
33
zhang@zhang-virtual-machine:~/Desktop$
2.字符串截取:

zhang@zhang-virtual-machine:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string:8}
nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string:8:8}
nickName
zhang@zhang-virtual-machine:~/Desktop$
3.字符串替换 :

zhang@zhang-virtual-machine:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string#my}
blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string#my blog}
nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string##my blog}
nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string##zi}
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~$ echo ${string%zi}
my blog nickName is xiao mu xi
zhang@zhang-virtual-machine:~$ echo ${string%%xi zi}
my blog nickName is xiao mu
zhang@zhang-virtual-machine:~$
zhang@zhang-virtual-machine:~/Desktop$ greet='hello,world,hello,linux'
zhang@zhang-virtual-machine:~/Desktop$ echo $greet
hello,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/hello/welcome to}
welcome to,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet//hello/welcome to}
welcome to,world,welcome to,linux
zhang@zhang-virtual-machine:~/Desktop$ echo $greet
hello,world,hello,linux

zhang@zhang-virtual-machine:~/Desktop$ echo $greet
hello,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/#world/php}
hello,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/#hello/welcome to}
welcome to,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/%linux/php}
hello,world,hello,php
zhang@zhang-virtual-machine:~/Desktop$
2.变量替换
表达式 含义
${var-DEFAULT} 如果 var 没有被声明,那么就以 DEFAULT 作为其默认值
${var:-DEFAULT} 如果var没有被声明或者其值为空, 那么就以 DEFAULT 作为其默认值
${var=DEFAULT} 如果var没有被声明,那么就以 DEFAULT 作为其默认值
${var:=DEFAULT} 如果var没有被声明或者其值为空,那么就以 DEFAULT 作为其默认值
${var+OTHERVALUE} 如果var声明了, 那么其值就是 OTHERVALUE ,否则就为空字符串
${var:+OTHERVALUE} 如果var被设置了, 那么其值就是 OTHERVALUE ,否则就为空字符串
${!varprefix*} 匹配之前所有以varprefix开头进行声明的变量
${!varprefix@} 匹配之前所有以varprefix开头进行声明的变量
zhang@zhang-virtual-machine:~/Desktop$ echo $name

zhang@zhang-virtual-machine:~/Desktop$ echo ${name-zhang}
zhang
zhang@zhang-virtual-machine:~/Desktop$ name=
zhang@zhang-virtual-machine:~/Desktop$ echo $name

zhang@zhang-virtual-machine:~/Desktop$ echo ${name:-zhang}
zhang
zhang@zhang-virtual-machine:~/Desktop$ echo ${name=zhang}

zhang@zhang-virtual-machine:~/Desktop$ echo ${name:=zhang}
zhang
zhang@zhang-virtual-machine:~/Desktop$
zhang@zhang-virtual-machine:~/Desktop$ echo $lastName

zhang@zhang-virtual-machine:~/Desktop$ echo ${lastName+guoping}

zhang@zhang-virtual-machine:~/Desktop$ lastName='xiao mu xi zi'
zhang@zhang-virtual-machine:~/Desktop$ echo ${lastName+guoping}
guoping
zhang@zhang-virtual-machine:~/Desktop$ echo ${lastName:+guoping}
guoping
zhang@zhang-virtual-machine:~/Desktop$
zhang@zhang-virtual-machine:~/Desktop$ varprefixName='zhang'
zhang@zhang-virtual-machine:~/Desktop$ varprefixLastName='guoping'
zhang@zhang-virtual-machine:~/Desktop$ fullName='guoping zhang'
zhang@zhang-virtual-machine:~/Desktop$ echo ${!varprefix*}
varprefixLastName varprefixName
zhang@zhang-virtual-machine:~/Desktop$
zhang@zhang-virtual-machine:~$ skillDynamicLanguage='php'
zhang@zhang-virtual-machine:~$ skillStaticLanguage='C++'
zhang@zhang-virtual-machine:~$ echo ${!Lanugage@}

zhang@zhang-virtual-machine:~$ echo ${!skill@}
skillDynamicLanguage skillStaticLanguage
zhang@zhang-virtual-machine:~$
以上所列,有点冷门(即用的比较少,有些系统脚本偶尔会用到),不过掌握后还是能省去不少麻烦。
————————————————
版权声明:本文为CSDN博主「小木兮子」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhang_referee/article/details/83511123

About the Author

今生在线

今生在线

发表评论

电子邮件地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据