views:

95

answers:

1

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
+1  A: 

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
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
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