表达式和运算符
条件表达式语句
[ 1 -eq 1 ]
[[ 1 -eq 1 ]]
test 1 -eq 1 等同于 [ 1 -eq 1 ]
[[email protected]_192-168-3-121 shell]# [ 1 -eq 1 ]
[[email protected]_192-168-3-121 shell]# echo $?
0
[[email protected]_192-168-3-121 shell]#
[[email protected]_192-168-3-121 shell]# [[ 1 -eq 1 ]]
[[email protected]_192-168-3-121 shell]# echo $?
0
[[email protected]_192-168-3-121 shell]#
[[email protected]_192-168-3-121 shell]# test 1 -eq 1
[[email protected]_192-168-3-121 shell]# echo $?
0
[[email protected]_192-168-3-121 shell]#
整数比较

字符串比较

注意:
使用 -z 或者 -n 判断字符串长度时,需要加双引号
使用 [ ... ] 时 需要给变量值加上双引号,不然判断会出错
使用 [[ ... ]] 时,则不需要

文件测试

布尔运算符

逻辑判断符

整数运算

其他运算工具

shell 不支持浮点数比较,可以借助bc来完成需求


shell括号用途总结
