有时候我们需要比较两个数字的大小关系,这时候就要用到关系运算符。关系运算符只支持数值运算,不支持字符运算。

#!/bin/bash
# author:陈树义
# site:www.chenshuyi.com

a=10
b=20

if [ $a -gt $b ]
then
echo "a great than b"
else
echo "a not great than b"
fi

上面输出:​​a not great than b​​。

除了支持大于运算符,Shell 语言还支持下面这些关系运算符:

  • ​-eq​​:检测两个数是否相等,相等返回 true。
  • ​-ne​​:检测两个数是否不相等,相等返回 true。
  • ​-gt​​:检测左边的数是否大于右边的,如果是返回 true。
  • ​-lt​​:检测左边的数是否小于右边的,如果是返回 true。
  • ​-ge​​:检测左边的数是否大于等于右边的,如果是返回 true。
  • ​-le​​:检测左边的数是否小于等于右边的,如果是返回 true。

Shell 入门教程(二十):关系运算符的使用_关系运算符