首页 > 代码库 > bash:裁剪字符串 ${var:3:2}

bash:裁剪字符串 ${var:3:2}

1)按照index和长度裁剪变量字符串
var=foobar echo ${var:3}  -------bar echo ${var:3:2} -------ba  从index为3开始,取两个
echo ${var: -4}

2)使用模式裁剪字符串

  6 phone=555-456-1414
  7 echo ${phone#*-}    456-1414  从开头匹配
  8 echo ${phone##*-}    1414    贪婪
  9 echo ${phone%-*}    555-456  从结尾匹配
 10 echo ${phone%%-*}     555    贪婪
 11 echo ${phone#-*}     555-456-1414

可以从复杂url中截取出域名

 

bash:裁剪字符串 ${var:3:2}