替换字符

基本类型 / 字符串

strV="1-3-2"

#a string can not be changed in place: glue a new one from slices
strV="${strV:0:2}2${strV:3}"
strV="${strV:0:4}3${strV:5}"
#strV is "1-2-3"
echo "strV is $strV"

#replace every '-' with '+'
plus=${strV//-/+}
#plus is "1+2+3"
echo "plus is $plus"

#tr replaces characters one for one: here 1 with 3 and 3 with 1
fromTr=$(tr '13' '31' <<< "$strV")
#fromTr is "3-2-1"
echo "fromTr is $fromTr"