On Mon, Sep 4, 2017 at 7:16 AM, Greg Reagle <greg.rea...@umbc.edu> wrote:

> On Sun, Sep 3, 2017, at 20:06, Mark Volkmann wrote:
> > IIUC, there isn't a command in fish that can be used to compare two
> strings to see if one is greater than the other in sorting order. For
> example, this doesn't work:
> >
> > set v1 'foo';
> > set v2 'bar';
> > if test $v1 > $v2
> >   ...
> > end
> >
> > Is there a recommended workaround for this?
>
> The wonderful (POSIX standard) command expr [1] will work.
>
> set v1 foo
> set v2 bar
> if expr $v1 '>' $v2 >/dev/null
>     echo true
> else
>     echo false
> end
>
> [1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/expr.html


Beware `expr`s automatic coercion of strings to ints. If you do `expr '234
< 1234' you'll get false despite "234" being lexically greater than "1234".
Which may be what you want but it's a potential gotcha if you really wanted
a string, not int, comparison.

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Fish-users mailing list
Fish-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fish-users

Reply via email to