help-bash
[Top][All Lists]
Advanced

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

Re: >= and <= for string comparison


From: Emanuele Torre
Subject: Re: >= and <= for string comparison
Date: Wed, 18 Jan 2023 15:54:02 +0100
User-agent: Mutt/2.2.9 (2022-11-12)

On Wed, Jan 18, 2023 at 07:52:26AM -0600, Peng Yu wrote:
> On 1/18/23, Lawrence Velázquez <vq@larryv.me> wrote:
> > On Wed, Jan 18, 2023, at 3:36 AM, Koichi Murase wrote:
> >> If we decide to support `<=' and `>=' for strings, to me, the cleanest
> >> solution seems to be reorganizing the lexical rule inside [[ ... ]] to
> >> accept <= and >= as single tokens though it would introduce
> >> incompatibilities. I'm currently not sure if it's worth it. We need
> >> more real use cases.
> >
> > And remember that, as Kerin pointed out at the beginning of this
> > thread, [[ ! x > y ]] and [[ ! x < y ]] are already available.
> > (They're decently portable, too.  The > and < operators are supported
> > by many test(1) implementations and will even be in the next version
> > of POSIX.)  So the threshold for introducing backward-incompatible
> > equivalents ought to be high.
> 
> I would never use [[ ! x > y ]] and [[ ! x < y ]] in my code. It will
> make the code hard to read. If I have to, I will use [[ x > y && x = y
> ]] and  [[ x < y && x = y ]]  instead.

That would be wrong; [[ x < y || x = "y" ]]  and  [[ x > y || x = "y" ]]
would be correct. (note that it is important that you double quote the
rhs of =/==/!= operator especially if it is a parameter expansion or
command substitution if you want it to be interpreted as a literal
comparison and not a pattern.)

I think you making this mistake shows that [[ ! x > y ]] and
[[ ! x < y ]] are actually easier to understand than combining < and =,
or < and =, but you can use whichever solution you prefer as long as it
is correct.  :)

Have a nice day.
 emanuele6



reply via email to

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