${#var}
例如
a="I am long"
echo ${#a}
切取字段
${string:position:length}或者
${string:position}
例如:
var="Welcome to the geekstuff"
echo ${var:15}
echo ${var:15:4}
刪除字符串
${string#substring} 刪除頭部開始計算的第一個符合要求的最短字符串
${string%substring} 刪除尾部開始計算的第一個符合要求的最短字符串
filename="bash.string.txt"
echo ${filename#*.}
echo ${filename%.*}
$ ./shortest.sh
After deletion of shortest match from front: string.txt
After deletion of shortest match from back: bash.string
${string##substring} 刪除頭部開始計算的符合要求的最長字符串
${string%%substring} 刪除尾部開始計算的符合要求的最長字符串
filename="bash.string.txt"
echo "After deletion of longest match from front:" ${filename##*.}
echo "After deletion of longest match from back:" ${filename%%.*}
$ ./longest.sh
After deletion of longest match from front: txt
After deletion of longest match from back: bash
獲取子串索引:
expr index $string
$substring
stringZ=abcABC123ABCabc
# 123456 ...
echo `expr index "$stringZ" C12` # 6
# C position.
echo `expr index "$stringZ" 1c` # 3
# 'c' (in #3 position) matches before '1'.
阅读(3462) | 评论(0) | 转发(0) |