bug-bash
[Top][All Lists]
Advanced

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

Re: -eq and strings


From: Reuti
Subject: Re: -eq and strings
Date: Mon, 6 Mar 2017 15:16:24 +0100

> Am 06.03.2017 um 15:09 schrieb Rob la Lau <rob@ohreally.nl>:
> 
> On 06-03-17 14:18, Greg Wooledge wrote:
>> You're misunderstanding.  In a math context, which you are creating here
>> by using -eq, the word 'x' is interpreted as a variable name, which may
>> contain another variable name, and so on, until finally an integer is
>> discovered.
> 
> Thanks. Now that I know it, I can indeed find it in the docs.
> 
> I guess this means that actually test and [ are 'broken':
> 
> $ test "x" -eq "x" && echo "yes" || echo "no"
> test: invalid integer 'x'
> no
> 
> $ [ "x" -eq "x" ] && echo "yes" || echo "no"
> [: invalid integer 'x'
> no
> 
> I hope this won't be fixed, as I rely quite heavily on this behaviour,
> lacking some other short 'is integer' test.

What about these:

if ! expr "$x" : '[[:digit:]]*$' >/dev/null; then echo no; fi

if [ -n "${x//[0-9]/}" ]; then echo no; fi

-- Reuti


> 
> Cheers,
>  Rob
> 
> --
> -- Rob la Lau
> --
> -- Sysadmin en webdeveloper in ruste
> --
> --      web : https://ohreally.nl/
> --      eml : rob@ohreally.nl
> --
> 

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail


reply via email to

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