${变量} # 返回变量值
${#变量} # 返回变量长度,字符长度
${变量:start} # 返回变量start数值之后的字符,包含start
${变量:start:length} # 提取start之后的length长度的字符,
${变量#word} # 从变量开头删除最短匹配的word子串
${变量##word} # 从变量开头删除最长匹配的word
${变量%word} # 从变量结尾删除最短的word
${变量%%word} # 从变量结尾删除最长匹配的word
# 替换
${变量/pattern/string} # 用string代替第一个匹配的pattern
${变量//pattern/string} # 用string代替所有的pattern
示例:
x="abcABC123ABCabc"
echo ${#x} # 15
echo ${x:3} # ABC123ABCabc
echo ${x:3:3} # 返回ABC
echo ${x#a*c} # 返回ABC123ABCabc(*表示通配符)
echo ${x##a*c} # 返回 (整个字符串被删空了)
echo ${x%a*c} # 返回abcABC123ABC
echo ${x%%a*c} # 返回 (整个字符串被删空了)
echo ${x/abc/cxk} # cxkABC123ABCabc
echo ${x//abc/cxk} # cxkABC123ABCcxk