bash shell 字符串提取,"${#string}"

$ cat stringexample1.sh #!/bin/sh FILEPATH=/home/w/wyoes/samples/kshsamples-v1.0.ksh echo '${FILEPATH} =' ${FILEPATH} # 字符串 filepath echo '${#FILEPATH} =' ${#FILEPATH} # 字符串的长度 echo '${FILEPATH%.} =' ${FILEPATH%.} # 截掉从右算起的第一个点号后的字符串 echo '${FILEPATH%%.} =' ${FILEPATH%%.} # 截掉从右算起的第二个点号后的字符串 echo '${FILEPATH%%/w} =' ${FILEPATH%%/w} # 截掉从右算起的第二个'/w'后的字符串 echo '${FILEPATH#///} =' ${FILEPATH#///} # 截掉从左算起的第三个斜线前的字符串包括斜线本身 echo '${FILEPATH##//} =' ${FILEPATH##//} # 截掉从左算起最后一个斜线前的字符串包括斜线本身

$ ./string_example1.sh
${FILEPATH}=/home/w/wyoes/samples/ksh_samples-v1.0.ksh
${#FILEPATH} = 42
${FILEPATH%.*}=/home/w/wyoes/samples/ksh_samples-v1.0
${FILEPATH%%.*}=/home/w/wyoes/samples/ksh_samples-v1
${FILEPATH%%/w*}=/home
${FILEPATH#/*/*/}=wyoes/samples/ksh_samples-v1.0.ksh
${FILEPATH##/*/}=ksh_samples-v1.0.ksh

参考:1.