首部子串替换与尾部子串替换
分别使用$键盘上两个邻居,#作为首部替换,%作为尾部替换。(形象记忆,无它)
bigorry@CTI2:~> echo ${bigorry/#b/B}(首部检验,成功)
Bigorry
bigorry@CTI2:~> echo ${bigorry/#i/I}(非首部,失败)
bigorry
bigorry@CTI2:~> echo ${bigorry/i/I}(普通的子串替换,成功)
bIgorry
bigorry@CTI2:~> echo ${bigorry/%ry/RY}(尾部检验,成功)
bigorRY
子串删除
bigorry@CTI2:~> bigorry="bigorry love linux"
bigorry@CTI2:~> echo ${bigorry/o}(单串删除)
bigrry love linux
bigorry@CTI2:~> echo ${bigorry//o}(全串删除)
bigrry lve linux
bigorry@CTI2:~> echo ${bigorry#*o}(从开头去除最短匹配前缀)
rry love linux
bigorry@CTI2:~> echo ${bigorry##*o}(从开头去除最长匹配前缀)
ve linux
bigorry@CTI2:~> echo ${bigorry%o*}(从尾部去除最短匹配后缀)
bigorry l
bigorry@CTI2:~> echo ${bigorry%%o*}从尾部去除最长匹配后缀)
big
字条截取
bigorry@CTI2:~> bigorry="what you want me learn"
bigorry@CTI2:~> echo ${bigorry:3:9}(提取第位4到第4+9位的字符串)
t you wan
长度计算
bigorry@CTI2:~> echo ${#bigorry}
22
利用已上知识解决文件名及后缀名
bigorry@CTI2:~> bigorry=/home/bigorry/ping.txt
bigorry@CTI2:~> basename $bigorry(获取文件名)
ping.txt
bigorry@CTI2:~> dirname $bigorry(获取目录名)
/home/bigorry
bigorry@CTI2:~> basename $bigorry .${bigorry##*.}(获取纯文件名)
ping
更强大的sed及正则表达式
bigorry@CTI2:~> echo $bigorry|sed "s/.*\.//"(获取后缀)
txt
bigorry@CTI2:~> echo $bigorry|sed "s/.*\///"(获取文件名)
ping.txt