help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: not sure about number comparison


From: Roger
Subject: Re: not sure about number comparison
Date: Sun, 19 Feb 2023 14:15:41 -0500

I like this method.

Any notes on POSIX compliance?  Or as it sounds if most are doing it, is it
already considered mostly POSIX compliant?

Think I've also been performing similar, except for maybe a few flip-flops
here and there.

Roger

On Sun, Feb 19, 2023 at 8:34 AM 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).
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]