Bash Shell - 数値比較演算子

BashシェルのInteger比較演算子を紹介します。

次の比較演算子を使用して、条件文でIntegerを比較できます。

Integer comparison Description Example
-eq is equal to if [ "$a" -eq "$b" ]
-ne is not equal to if [ "$a" -ne "$b" ]
-gt is greater than if [ "$a" -gt "$b" ]
-ge is greater than or equal to if [ "$a" -ge "$b" ]
-lt is less than if [ "$a" -lt "$b" ]
-le is less than or equal to if [ "$a" -le "$b" ]
< is less than (("$a" < "$b"))
<= is less than or equal to (("$a" <= "$b"))
> is greater than (("$a" > "$b"))
>= is greater than or equal to (("$a" >= "$b"))

Example 1

-eqと同じ形式の比較演算子でIntegerを比較する例です。

#!/bin/bash

a=10
b=10

if [ "$a" -eq "$b" ]
then
    echo "a is equal to b"
fi

Output:

$ bash example.sh
a is equal to b

Example 2

<と同じ形式の比較演算子でIntegerを比較する例です。

#!/bin/bash

c=10
d=20

if (( "$c" < "$d" ))
then
    echo "c is less than d"
fi

Output:

$ bash example.sh
c is less than d
codechachaCopyright ©2019 codechacha