Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[ a \< b ] and [ a \> b ]

20 views
Skip to first unread message

Hongyi Zhao

unread,
Nov 9, 2017, 7:43:24 PM11/9/17
to
Hi,

Do the following test:


werner@debian-01:~$ [ 1 \> 2 ]; echo $?
1
werner@debian-01:~$ [ 1 \< 2 ]; echo $?
0

So, it seems the above are equivalent to the following ones:

werner@debian-01:~$ [ 1 -gt 2 ]; echo $?
1
werner@debian-01:~$ [ 1 -lt 2 ]; echo $?
0

But, this is not mentioned in the test's manpage. Any hints?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Kaz Kylheku

unread,
Nov 9, 2017, 7:46:56 PM11/9/17
to
On 2017-11-10, Hongyi Zhao <hongy...@gmail.com> wrote:
> Hi,
>
> Do the following test:
>
>
> werner@debian-01:~$ [ 1 \> 2 ]; echo $?
> 1
> werner@debian-01:~$ [ 1 \< 2 ]; echo $?
> 0
>
> So, it seems the above are equivalent to the following ones:
>
> werner@debian-01:~$ [ 1 -gt 2 ]; echo $?
> 1
> werner@debian-01:~$ [ 1 -lt 2 ]; echo $?
> 0
>
> But, this is not mentioned in the test's manpage. Any hints?

I'm surprised that you still don't understand that there exist [ and
test programs in your /usr/bin, and that like-named utilities are also
built into your shell.

In Bash you can type "help test", which documents the built-in one
and all of its operators.

The man page is likely documenting your /usr/bin/test;
but that's not what you're using above in your commands.

In short, what happens if you try this:

/usr/bin/[ 1 \> 2 ]

Chris Elvidge

unread,
Nov 10, 2017, 8:20:49 AM11/10/17
to
On 10/11/2017 12:43 am, Hongyi Zhao wrote:
> Hi,
>
> Do the following test:
>
>
> werner@debian-01:~$ [ 1 \> 2 ]; echo $?
> 1
> werner@debian-01:~$ [ 1 \< 2 ]; echo $?
> 0
>
> So, it seems the above are equivalent to the following ones:
>
> werner@debian-01:~$ [ 1 -gt 2 ]; echo $?
> 1
> werner@debian-01:~$ [ 1 -lt 2 ]; echo $?
> 0
>
> But, this is not mentioned in the test's manpage. Any hints?
>
> Regards
>
1 comes before 2 lexically, and is also less than 2 arithmetically.
Tests not equivalent; just happen to give same result. Try [ 10 \< 2 ].



--

Chris Elvidge, England

Hongyi Zhao

unread,
Nov 10, 2017, 8:41:33 AM11/10/17
to
On Fri, 10 Nov 2017 13:20:42 +0000, Chris Elvidge wrote:

> 1 comes before 2 lexically, and is also less than 2 arithmetically.
> Tests not equivalent; just happen to give same result. Try [ 10 \< 2 ].

Thanks for your notes.

But I still can't figure out how the OS do the lexicographical comparison
for this case.

From the `` help test '', I can find the following explains:

STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically.

Say for the example you given above:

[ 10 \< 2 ]

In this case, string 10 including 2 characters while 2 only has one
character, how the shell/OS do the lexical comparison for this case?

Chris Elvidge

unread,
Nov 10, 2017, 8:51:07 AM11/10/17
to
The 1 in 10 still comes before 2 lexically - lexical comparisons are
done on a character by character basis until one runs out of characters
to compare. Try [ 20 \< 2 ]


--

Chris Elvidge, England

Thomas 'PointedEars' Lahn

unread,
Nov 10, 2017, 4:52:32 PM11/10/17
to
Hongyi Zhao wrote:

> But I still can't figure out how the OS do the lexicographical comparison
> for this case.

It is not the OS, but the the shell, and it is a common mechanism in
programming languages. Look up “collation”.

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. /Bitte keine Kopien per E-Mail.

Jim Beard

unread,
Nov 10, 2017, 7:44:12 PM11/10/17
to
On Fri, 10 Nov 2017 22:52:25 +0100, Thomas 'PointedEars' Lahn wrote:

> Hongyi Zhao wrote:
>
>> But I still can't figure out how the OS do the lexicographical
>> comparison for this case.
>
> It is not the OS, but the the shell, and it is a common mechanism in
> programming languages. Look up “collation”.

More precisely, look up ASCII collation, or perhaps C-locale collation Or
Pinyin collation.
Chinese collation, involving ordering of the representations of
characters, is quite different.

Cheers!

jim b.

--
UNIX is not user-unfriendly, it merely expects users to be computer-
friendly.

Hongyi Zhao

unread,
Nov 11, 2017, 1:59:21 AM11/11/17
to
On Sat, 11 Nov 2017 00:44:07 +0000, Jim Beard wrote:

> More precisely, look up ASCII collation, or perhaps C-locale collation
> Or Pinyin collation.
> Chinese collation, involving ordering of the representations of
> characters, is quite different.

How to look the collation? What's the relationship between the collation
and the following variable:

LC_COLLATE

For my case, the locale command gives the following result:

werner@debian-01:~$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

Jim Beard

unread,
Nov 11, 2017, 9:51:03 AM11/11/17
to
The collation standard for your case is determined by the LC_COLLATE,
i.e. en_US.UTF-8. The Default Unicode Collation Element Table (DUCET)
would be of particular importance to you.

http://www.unicode.org/reports/tr10/

Hongyi Zhao

unread,
Nov 11, 2017, 7:37:28 PM11/11/17
to
On Sat, 11 Nov 2017 14:50:57 +0000, Jim Beard wrote:

> The collation standard for your case is determined by the LC_COLLATE,
> i.e. en_US.UTF-8. The Default Unicode Collation Element Table (DUCET)
> would be of particular importance to you.
>
> http://www.unicode.org/reports/tr10/

Thanks a lot.
0 new messages