140

In bash, what's the difference, if any, between the equal and double equal test operators?

[[ "a" = "a" ]] && echo equal || echo not-equal
[[ "a" == "a" ]] && echo equal || echo not-equal
[[ "a" = "b" ]] && echo equal || echo not-equal
[[ "a" == "b" ]] && echo equal || echo not-equal

results in:

equal
equal
not-equal
not-equal
brianegge
  • 29,240
  • 13
  • 74
  • 99

1 Answers1

131

There's no difference, == is a synonym for = (for the C/C++ people, I assume). See here, for example.

You could double-check just to be really sure or just for your interest by looking at the bash source code, should be somewhere in the parsing code there, but I couldn't find it straightaway.

schnaader
  • 49,103
  • 10
  • 104
  • 136
  • 26
    There's no difference for *string* comparisons, but you can't use `=` for numeric comparisons in `(())` (you must use `==` in `(())` or `-eq` in `[]`, `test` or `[[]]`. See my answer [here](http://stackoverflow.com/questions/2600281/what-is-the-difference-between-operator-and-in-bash/2601583#2601583). – Dennis Williamson Jul 16 '10 at 20:52
  • 24
    It's also worth noting that == was introduced in bash, but bourne shell does not support it. In some systems, you'll notice that /bin/sh is actually bash, and in other systems, it's bourne. I ran into that problem when a shell script worked correctly on multiple systems, but failed on one. The == being unsupported in bourne was the reason it failed on the one. – Joe Aug 24 '10 at 00:12
  • 4
    Note, in ksh if you check the syntax, you get a depreciation warning with the single = syntax. `warning: line 3: '=' obsolete, use '=='` – brianegge Aug 08 '13 at 00:24
  • 6
    There's no difference IN BASH. `==` does not work in all shells, hence not portable. Prefer `=`. – mak Sep 18 '16 at 20:38
  • As @mak pointed out, == doesn't work in all shells, I confirmed that in Ubuntu 16.04 == doesn't work. – Yingpei Zeng Mar 16 '17 at 01:26