这篇博客和我之前整理的字符串处理有部分重叠 https://www.cnblogs.com/dalianpai/p/12702237.html
但是主要字符串的特换还是有用,就转载了一下。转载:https://blog.csdn.net/zhang_referee/article/details/83511123
1.字符串操作
表达式 | 含义 |
---|---|
${#string} | 获取字符串 string 的长度 |
${string:position} | 从字符串 string position 处截取子串 |
${string:position:length} | 从字符串 string position 处截取length 长度的子串 |
${string#substring} | 从字符串 string 的开头,删除最短匹配 substring 的子串 |
${string##substring} | 从字符串 string 的开头,删除最长匹配 substring 的子串 |
${string%substring} | 从字符串 string 的结尾,删除最短匹配 substring 的子串 |
${string%%substring} | 从字符串 string 的结尾,删除最长匹配 substring 的子串 |
${string/substring/replacement} | 在字符串 string 中,使用 replacement 替换 substring |
${string//substring/replacement} | 在字符串 string 中,使用 replacement,代替所有匹配的 substring |
${string/#substring/replacement} | 条件替换 : 如果字符串 string 的前缀匹配 substring , 那么就用 replacement 来代替匹配到的 substring |
${string/%substring/replacement} | 条件替换 : 如果字符串 string 的后缀匹配 substring ,那么就用 replacement 来代替匹配到的 substring |
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:~$
以上所列,有点冷门(即用的比较少,有些系统脚本偶尔会用到),不过掌握后还是能省去不少麻烦。