Bash の数値を比較する
Fumbani Banda
2023年1月30日
Bash

このチュートリアルでは、角括弧 []
と二重括弧-(( ))
を使用して bash の数値を比較します。
角括弧 []
を使用して Bash の数値を比較する
比較演算子は、角括弧内で使用する必要があります。
x=4
y=3
if [ $x -eq $y ];
then
echo $x and $y are equal
elif [ $x -gt $y ]
then
echo $x is greater than $y
else
echo $x is less than $y
fi
出力:
4 is greater than 3
二重括弧 (( ))
を使用して、Bash の数値を比較する
比較演算子は、二重括弧内で使用する必要があります。
x=4
y=10
if (( $x < $y ))
then
echo $x is less than $y
elif (( $x > $y ))
then
echo $x is greater than $y
else
echo $x is equal to $y
fi
出力:
4 is less than 10
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
著者: Fumbani Banda