Pau*_*ce. 82
您必须==在以下数字比较中使用(( ... )):
$ if (( 3 == 3 )); then echo "yes"; fi
yes
$ if (( 3 = 3 )); then echo "yes"; fi
bash: ((: 3 = 3 : attempted assignment to non-variable (error token is "= 3 ")
Run Code Online (Sandbox Code Playgroud)
您可使用在字符串比较[[ ... ]]或[ ... ]或test:
$ if [[ 3 == 3 ]]; then echo "yes"; fi
yes
$ if [[ 3 = 3 ]]; then echo "yes"; fi
yes
$ if [ 3 == 3 ]; then echo "yes"; fi
yes
$ if [ 3 = 3 ]; then echo "yes"; fi
yes
$ if test 3 == 3; then echo "yes"; fi
yes
$ if test 3 = 3; then echo "yes"; fi
yes
Run Code Online (Sandbox Code Playgroud)
"字符串比较?",你说?
$ if [[ 10 < 2 ]]; then echo "yes"; fi # string comparison
yes
$ if (( 10 < 2 )); then echo "yes"; else echo "no"; fi # numeric comparison
no
$ if [[ 10 -lt 2 ]]; then echo "yes"; else echo "no"; fi # numeric comparison
no
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12428 次 |
| 最近记录: |