shell变量的删除与替换

在读鸟哥的私房菜的时候,我学习到了shell变量的删除与替换方法,删除方法之前接触过,而替换方法则是之前没有接触过的

shell变量的删除与替换

我们用一些例子来说明一下上述的用法

astr='good_good'
echo ${astr#*_}
good
echo ${astr#good}
_good

echo ${astr%good}
good_
echo ${astr/good/bad}
bad_good
echo ${astr//good/bad}
bad_bad

 而两个##或者两个%%则是要在有*的时候才能发挥作用,它代表贪婪匹配

astr='/a/b/c'
echo ${astr##*/}
c
echo ${astr#*/}
a/b/c