1. string长度
# test.sh
hello=world
echo "hello's length:" ${#hello}
$ ./test.sh
hello's length: 5
2. substr
#!/bin/bash
hello=world
echo "hello的子串一:" ${hello:1}
echo "hello的子串一:" ${hello:1:3}
$ ./test.sh
hello的子串一: orld
hello的子串一: orl
3. 删除最小匹配子串
#! /bin/bash
filename="front.middle.back"
# 从前面开始匹配,删除最短匹配
echo ${filename#*.}
# 从后面开始匹配,删除最短匹配
echo ${filename%.*}
$ ./shortest-match.sh
middle.back
front.middle
4. 删除最长匹配
#! /bin/bash
filename="front.middle.back"
# 从前面开始匹配,删除最长匹配
echo ${filename##*.}
# 从后面开始匹配,删除最长匹配
echo ${filename%%.*}
$ ./longest-match.sh
back
front
5. 替换string中的一段
#!/bin/bash
filename="front.middle.back.middle.back"
echo "只替换第一个middle:" ${filename/middle/abcd}
echo "替换所有middle:" ${filename//middle/abcd}
echo "只替换在开头出现的front:" ${filename/#front/abcd}
echo "只替换在结尾出现的back:" ${filename/%back/abcd}
$ ./replacement.sh
只替换第一个middle: front.abcd.back.middle.back
替换所有middle: front.abcd.back.abcd.back
只替换在开头出现的front: abcd.middle.back.middle.back
只替换在结尾出现的back: front.middle.back.middle.abcd