1.长度
[web97@salewell97 ~]$ test='I love china'
[web97@salewell97 ~]$ echo ${#test}
12
${#变量名}得到字符串长度
另一种方法:
expr length "$adcode"
2.截取字串
[chengmo@localhost ~]$ test='I love china'
[chengmo@localhost ~]$ echo ${test:5}
e china
[chengmo@localhost ~]$ echo ${test:5:10}
e china
echo ${test:(-3)}
ina
echo ${test:(-3):2}
in
${变量名:起始:长度}得到子字符串
3.字符串删除
[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test#/}
c:/windows/boot.ini
[chengmo@localhost ~]$ echo ${test#*/}
windows/boot.ini
[chengmo@localhost ~]$ echo ${test##*/}
boot.ini
[chengmo@localhost ~]$ echo ${test%/*}
c:/windows
[chengmo@localhost ~]$ echo ${test%%/*}
c:
${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。
${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。
注意:${test##*/},${test%/*} 分别是得到文件名,或者目录地址最简单方法。
4.字符串替换
[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test//}
c:/windows/boot.ini
[chengmo@localhost ~]$ echo ${test///}
c:/windows/boot.ini
${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”//”表示。