bug-bash
[Top][All Lists]
Advanced

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

-eq and strings


From: Rob la Lau
Subject: -eq and strings
Date: Sat, 4 Mar 2017 13:15:58 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Thunderbird/45.7.1

Hello,

I'm not sure whether I found a bug in Bash or in it's documentation.

The documentation suggests that the arithmetic binary operator '-eq' is
for integers ("Arg1 and arg2 may be positive or negative integers."),
but when using it with '[[ ]]' it doesn't fail on strings.

$ [[ 1 -eq 1 ]] && echo "yes" || echo "no"
yes

$ [[ "x" -eq "x" ]] && echo "yes" || echo "no"
yes

as opposed to `test':

$ test "x" -eq "x" && echo "yes" || echo "no"
test: invalid integer 'x'
no

$ [ "x" -eq "x" ] && echo "yes" || echo "no"
[: invalid integer 'x'
no

If this is expected behaviour, I think the documentation should reflect
this.

Use case: I use this to test whether a variable is an integer.

$ [ "${x}" -eq "${x}" ] 2> /dev/null && echo "yes" || echo "no"

...

Looking a bit further: it doesn't do any comparison on the given strings:

$ [[ "x" -eq "y" ]] && echo "yes" || echo "no"
yes

$ [[ "x" -eq "yz" ]] && echo "yes" || echo "no"
yes

but it does on integers:

$ [[ 1 -eq 2 ]] && echo "yes" || echo "no"
no

Kind regards,
  Rob

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



reply via email to

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