[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: not sure about number comparison
From: |
Greg Wooledge |
Subject: |
Re: not sure about number comparison |
Date: |
Sun, 19 Feb 2023 08:34:35 -0500 |
On Sun, Feb 19, 2023 at 08:44:12AM +0800, winnie hw wrote:
> 1. for number comparison, when to use "-ne" and when to use "!="?
In addition to David's answers, bash also has the (( )) command, which
is preferred for numeric comparisons.
x=42
if ((x < 5)); then
echo "too small"
elif ((x > 20)); then
echo "too big"
else
echo "just right"
fi
For bash scripts, many people advocate (( )) for numeric stuff, [[ ]] for
string stuff, and not to use [ ] at all. Following that advice will
help make it clear which operations are being used. They also offer
the "prettiest" forms of code syntax for their respective operations
(you can omit the quotes inside [[ ]] most of the time, and you can omit
the quotes and the dollar signs inside (( )) most of the time).