[root@localhost ~]# var=12345678
//字符串长度
[root@localhost ~]# echo ${#var}
8
//截取字符串
${parameter:offset}
${parameter:offset:length}
[root@localhost ~]# echo ${var:4}
5678
[root@localhost ~]# echo ${var:4:2}
56
[root@localhost ~]# echo ${var:(-3):(-1)}
67
//替换字符串
格式:${parameter/pattern/string}
[root@localhost ~]# echo ${var/123/888}
88845678
[root@localhost ~]# test="hello world world"
[root@localhost ~]#
[root@localhost ~]# echo ${test/world/}
hello world
[root@localhost ~]# echo ${test//world/} //patterm前面开头一个正斜杠为只匹配第一个字符串,两个正斜杠为匹配所有字符。
hello
//字符串截取
${parameter#word} # 删除匹配前缀
${parameter##word}
${parameter%word} # 删除匹配后缀
${parameter%%word}
# 去掉左边,最短匹配模式,##最长匹配模式。
% 去掉右边,最短匹配模式,%%最长匹配模式。
[root@localhost ~]# url="https://www.cnblogs.com/lonecloud/p/9066199.html"
[root@localhost ~]# echo ${url#*//} //*代表匹配任意字符,以//为分隔符去掉左边,截取右边字符串
www.cnblogs.com/lonecloud/p/9066199.html
[root@localhost ~]# echo ${url##*/} //以/为分隔符,最长匹配删除左边到/字符串,截取剩余部分
9066199.html
[root@localhost ~]# echo ${url%%//*} //以//为分隔符,从右边最长匹配删除
https:
[root@localhost ~]# echo ${url##*.}
html
来源:CSDN
作者:wo1769815
链接:https://blog.csdn.net/wo1769815/article/details/104108489