语法1: substring=${string:start:len}
string的下标从0开始,以start可是,截取len个字符,并赋值于substring
1 #!/bin/bash2 #substr=${ string:start:len}3 str="123456789"4 substr=${str:3:3}5 echo $substr6 7 输出:8 456
语法2:截取指定字符串
${varible##*string}:从左向右截取最后一个string后的字符串
${varible#string*}:从左向右截取第一个string后的字符串 ${varible%%string*}:从右向左截取最后一个string后的字符串 ${varible%string*}:从右向左截取第一个string后的字符串 “*”:只是一个通配符可以不要1 #!/bin/bash 2 3 str="usr/bin/user" 4 #* ${varible##*string} 5 substr1=${str##*/} 6 echo $substr1 # = user 7 8 #* ${varible#*string} 9 substr2=${str#*/}10 echo $substr2 # = bin/user11 12 #* ${varible%%string*}13 substr3=${str%%/*}14 echo $substr3 # = usr 15 16 #* ${varible%string*}17 substr4=${str%/*}18 echo $substr4 # = usr/bin