2

What is the difference between == and EQU operator in Batch?

Below is just a sample snippet:

if !one! EQU - (
    if !one!==!two! (
        if !two!==!three! (
            goto endgame
        )
    )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 3
    Best advice is to generally use `EQU`, `NEQ`, `GTR`, `GEQ`, `LSS`, and `LEQ`, when you are 100% certain that the values you're comparing are integer strings, otherwise just forget about it. In cmd.exe all variables are of string type anyhow, so use the correct syntax for comparing them `IF "!one!" == "!two!" …`, or `IF /I "!one!" == "!two!" …` and/or `IF NOT "!one!" == "!two!" …`, or `IF /I NOT "!one!" == "!two!" …`. – Compo Jan 19 '22 at 18:08
  • 3
    That is explained in __full__ details (really full) in my answer on [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564). This answer contains also a link to [weird results with IF](https://stackoverflow.com/questions/35769581/weird-results-with-if) with C code demonstrating the behavior of `EQU` using nearly the same functions as `cmd.exe`. – Mofi Jan 19 '22 at 19:11
  • 1
    At a `cmd` prompt, use the command `IF /?` which will explain the comparison operators. – lit Jan 20 '22 at 02:11

1 Answers1

4

If EQU finds that the two things being compared are valid integers (in octal, decimal, or hexadecimal), then an integer comparison will be performed.

For example, if 0x64 EQU 100 (echo yes) else (echo no) returns yes because 64 in hexadecimal is equivalent to 100 in decimal.

If either of the two things being compared cannot possibly be a valid integer (the number 09, for example, which would be octal except that 9 isn't a valid octal digit), then a string comparison is performed.

== only runs a string comparison, so if 0x64==100 (echo yes) else (echo no) returns no because the two strings are different.

In terms of simple string comparisons, the two operators act in essentially the same way, but EQU takes a few clock cycles longer because it first has to try to convert the two items to integers.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55