3

I was writing some "if then" statements and found what seemed to me an odd behavior. Upon investigation I realized that it boiled down to the exit code of the comparison I was making. I illustrate my findings in the following code snippet.

As you can see

rc=1
[ $rc -eq 0 ]
es_num=$?
[ $rc=0 ]
es_str=$?
echo "es_num is $es_num"
echo "es_str is $es_str"

Outputs

es_num is 1
es_str is 0

Is there any documentation, preferably from the POSIX standards, that talks about the difference in the exit statuses of -eq and = in a test construct?

What should I be aware of when writing conditional statements? What are some best practices regarding this?

Portable code is preferable to Bash code (which I'm using).

  • I didn't know about the space being necessary. What I meant with "odd" is "weird if you read as pseudo code, ignoring the quirks of the language". Thanks. @ilkkachu – Elegance Mar 7 at 19:16
  • 1
    The only way I can get [ $rc=0 ] to fail with rc=1 is to set IFS to 1 as well. That would cause both tests to error out and set $? to 2 (in bash). – Kusalananda Mar 7 at 19:25
  • @ilkkachu There was a typo. Of course I can't ignore them, but if I knew them, I wouldn't have to ask. That's exactly the point of the question. – Elegance Mar 7 at 21:29
  • @Elegance, ok, good, thanks. And yes, you're right, you wouldn't have to ask if you knew. It's just that even a typo like that can send the readers off in the wrong direction, looking for some really weird edge case that could explain the result. (The shell can be a bit quirky sometimes so there might have been a remote possibility of an edge case where both would return 1...) – ilkkachu Mar 7 at 21:52
  • @ilkkachu Thank you for the helpful feedback. – Elegance Mar 7 at 22:08
3

-eq

True if the integers n1 and n2 are algebraically equal; otherwise, false.

test

=

True if the strings s1 and s2 are identical; otherwise, false.

test

So -eq compares integers and = compares strings (which will also work with some limited integer cases).


You do have a syntax issue though, it should be:

[ "$rc" = 0 ]

And not

[ $rc=0 ]

[ "$rc" = 0 ] should exit with 1 because rc does not equal 0

[ $rc=0 ] should actually exit with 0 because it's likely going to be treated as a string and the presence of a string within the [ test construct will evaluate to true


With the sh [ test there are a few differences:

# leading 0
$ [ 01 -eq 1 ]; echo $?
0
# adjacent whitespace
$ [ ' 1' -eq 1 ]; echo $?
0
# negative 0 vs positive 0
$ [ 0 -eq -0 ]; echo $?
0

However with the bash [[ test there are a large number of differences (Including the ones mentioned above):

# base 8
$ [[ 032 -eq 26 ]]; echo $?
0
# Arithmetic expressions
$ [[ 1*6+32/15*2-1 -eq 9 ]]; echo $?
0
# Base 64
$ [[ 64#Hello_world -eq -5506400892957379251 ]]; echo $?
0
  • I'm just a bit confused about what is meant by numeric and string comparisons. Let's say I use = to compare numbers. When is it not going to not yield the same result as a numeric comparison? – Elegance Mar 7 at 19:17
  • Any examples with strings that have nothing but digits, and don't have leading zeros? – Elegance Mar 7 at 19:21
  • 1
    @Elegance The point is that with -eq, the left and right hand side are compared as integers, not as strings. The test would evaluate the strings as integers (probably by passing them through strtol() or some such C function) before carrying out the comparison. – Kusalananda Mar 7 at 19:43
  • 1
    @Elegance In a bash test with [[ ... -eq ... ]] you could even have arithmetic calculations on either side, as in [[ 1+1 -eq 2 ]]. Obviously, [[ 1+1 == 2 ]] would not be true. – Kusalananda Mar 7 at 20:02
  • @Jesse_b You shared the same link twice. I take it the first one is incorrect. – Elegance Mar 7 at 22:09
0

For numeric comparisons you have to use -eq whereas = is for string comparisons (as from your variable naming you already seem to know).

One of the best introductions on the test aka [ command I know is The Unix Shell's Humble If

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.