bash string equality [duplicate]

2019-01-30 07:45发布

问题:

This question already has an answer here:

  • What is the difference between operator “=” and “==” in Bash? 2 answers

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:

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.