替换子串

基本类型 / 字符串

startString="3, 2, 1, go!"

#${var//from/to} 替换所有出现的位置,${var/from/to} 只替换第一处
startString=${startString//1/one}
startString=${startString//2/two}
startString=${startString//3/three}
#startString is "three, two, one, go!"

#对整个文件或流,同样的工作由 sed 完成
fromSed=$(echo "3, 2, 1, go!" | sed 's/1/one/g; s/2/two/g; s/3/three/g')
#fromSed is "three, two, one, go!"

echo "startString is $startString"
echo "fromSed is $fromSed"