字符串操作
${#string}输出字句的字符个数
${string:position:length}输出$position开始的length长度的字符
${string:position}
[[email protected] scripts]# liuyifei="i like sanpan i like luo swimming" [[email protected] scripts]# echo ${#liuyifei} 33 [[email protected] scripts]# echo ${liuyifei:2:4} like [[email protected] scripts]# echo ${liuyifei:2} like sanpan i like luo swimming ====以上实测 ${string#substring}从string中从前往后检索,只要匹配到字符就开始删除最短匹配 ${string##substring}从头匹配,开始最长删除 ${string%substring}从结尾开始 ${string%%substring}从结尾开始,类似以上## ======以上必须必须从一开始就匹配上 [[email protected] scripts]# echo ${liuyifei#like} i like sanpan i like luo swimming [[email protected] scripts]# echo ${liuyifei#*like} sanpan i like luo swimming [[email protected] scripts]# echo ${liuyifei##*like} luo swimming [[email protected] scripts]# echo ${liuyifei%*like} i like sanpan i like luo swimming [[email protected] scripts]# echo ${liuyifei%like} i like sanpan i like luo swimming [[email protected] scripts]# echo ${liuyifei%like*} i like sanpan i [[email protected] scripts]# echo ${liuyifei%%like*} i
========================
${string/substring/replace}替换第一个遇到的匹配的字符
${string/#substring/replace}前缀匹配,必须全部匹配上,替换一长条
${string/%substring/replace}从后往前,替换最后一个遇到的匹配字符,
[[email protected] scripts]# echo ${liuyifei/like/love}
i love sanpan i like luo swimming
[[email protected] scripts]# echo ${liuyifei/#*like/love}
love luo swimming
[[email protected] scripts]# echo ${liuyifei/%like*/love}
i love
时间: 07-07