1 變數進階
2 運算
3 判斷式
4 迴圈
String Length
- ${#string}
- expr length $string
- These are the equivalent of strlen() in C.
- expr "$string" : '.*'
stringZ=abcABC123ABCabc echo ${#stringZ} # 15 echo `expr length $stringZ` # 15 echo `expr "$stringZ" : '.*'` # 15
Substring Extraction
- ${string:position}
- Extracts substring from $string at $position.
If the $string parameter is "*" or "@", then this extracts the positional parameters, [1] starting at $position. - ${string:position:length}
- Extracts $length characters of substring from $string at $position.
stringZ=abcABC123ABCabc # 0123456789..... # 0-based indexing. echo ${stringZ:0} # abcABC123ABCabc echo ${stringZ:1} # bcABC123ABCabc echo ${stringZ:7} # 23ABCabc echo ${stringZ:7:3} # 23A # Three characters of substring. # Is it possible to index from the right end of the string? echo ${stringZ:-4} # abcABC123ABCabc # Defaults to full string, as in ${parameter:-default}. # However . . . echo ${stringZ:(-4)} # Cabc echo ${stringZ: -4} # Cabc # Now, it works. # Parentheses or added space "escape" the position parameter. # Thank you, Dan Jacobson, for pointing this out.
+ - * /
a=3
b=10
let c=$a+$b
echo $c
... 13
let d=$b-$a
echo $d
... 7
除了使用 let 也可使用
c=$(($a+$b))
c=`echo $a + $b | bc`
c=`expr $a + $b`
echo $c
13
沒有留言:
張貼留言