7种方法,用到的工具有:awk,sed,rev,cut,grep,colrm,expr,是学习这些工具的很好例子。
1.echo string|awk '{print substr($0,length($0)-1,length($0))}' #把-1改成-0就是取最末位值2.echo string|sed 's/(.*)(..$)/2/'3.echo string|rev|cut -c-2|rev4.echo ${var:$((${#var}-2)):2}或echo ${var:$((-2))}5.echo string|grep -o ..$6.echo string|rev|colrm 3|rev7.expr string : ".*(..)$"