63

It seems that these two operators are pretty much the same - is there a difference? When should I use = and when ==?

2 Answers 2

85

You must use == in numeric comparisons in (( ... )):

$ if (( 3 == 3 )); then echo "yes"; fi yes $ if (( 3 = 3 )); then echo "yes"; fi bash: ((: 3 = 3 : attempted assignment to non-variable (error token is "= 3 ") 

You may use either for string comparisons in [[ ... ]] or [ ... ] or test:

$ if [[ 3 == 3 ]]; then echo "yes"; fi yes $ if [[ 3 = 3 ]]; then echo "yes"; fi yes $ if [ 3 == 3 ]; then echo "yes"; fi yes $ if [ 3 = 3 ]; then echo "yes"; fi yes $ if test 3 == 3; then echo "yes"; fi yes $ if test 3 = 3; then echo "yes"; fi yes 

"String comparisons?", you say?

$ if [[ 10 < 2 ]]; then echo "yes"; fi # string comparison yes $ if (( 10 < 2 )); then echo "yes"; else echo "no"; fi # numeric comparison no $ if [[ 10 -lt 2 ]]; then echo "yes"; else echo "no"; fi # numeric comparison no 
Sign up to request clarification or add additional context in comments.

2 Comments

You should not use == with [ or test, though. == is not part of the POSIX specification, and will not work with all shells (dash, in particular, does not recognize it).
@chepner: That's true, but the question is specifically about Bash.
30

There's a subtle difference with regards to POSIX. Excerpt from the Bash reference:

string1 == string2
True if the strings are equal. = may be used in place of == for strict POSIX compliance.

1 Comment

No difference in bash though? Just a portability issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.