[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: not sure about number comparison
From: |
winnie hw |
Subject: |
Re: not sure about number comparison |
Date: |
Mon, 20 Feb 2023 08:54:47 +0800 |
Thanks David & Greg for the lessons.
regards.
On Sun, Feb 19, 2023 at 9:34 PM Greg Wooledge <greg@wooledge.org> wrote:
> 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).
>
>