用法·
-
雙園括號
(( ... )):- 用於算術表達式。
- 支持標準算術運算符,如
,,,,,,。>=>==!=
-
單方括號
[ ... ]和雙括號[[ ... ]]:- 用於測試表達式。
- 支持數字比較運算符,如
-le,,,,,,。-lt-ge-gt-eq-ne
示例 腳本
#!/usr/bin/bash
x=3
# Using (( ... )) for arithmetic comparison
if (( x
echo "x is less than or equal to 5"
fi
# Using [ ... ] for numeric comparison
if [ $x -le 5 ]; then
echo "x is less than or equal to 5"
fi
# Using [[ ... ]] for numeric comparison
if [[ $x -le 5 ]]; then
echo "x is less than or equal to 5"
fi
以下 , 單 雙方括號 都可以
-eq 等於,如:if [ "$a" -eq "$b" ]
-ne 不等於,如:if [ "$a" -ne "$b" ]
-gt 大於,如:if [ "$a" -gt "$b" ]
-ge 大於等於,如:if [ "$a" -ge "$b" ]
-lt 小於,如:if [ "$a" -lt "$b" ]
-le 小於等於,如:if [ "$a" -le "$b" ]
以下 全部 需要圓雙括號
> 大於(需要雙括號),如:(("$a" > "$b"))
>= 大於等於(需要雙括號),如:(("$a" >= "$b"))