Read that for comparing strings inside if we need to use double square brackets. Some books says that comparison can be done by =. But it works with the == too.

if [[ $a == $b ]];then
    echo "equal"

Is there a difference between = and == in the comparison?

  • 4
    Is there a question in here somewhere? If so, I'm not seeing it. = is for [. == is for [[. – Chris Down Apr 11 '13 at 5:35

[[ $a == $b ]] is not comparison, it's pattern matching. You need [[ $a == "$b" ]] for byte-to-byte equality comparison. = is the same as == in any shell that supports [[...]] (introduced by ksh).

[[...]] is not standard sh syntax. The [ command is standard, and the standard comparison operator there is = (though some [ implementations also recognise ==).

Just like in any argument to any command, variables must be quoted, so:

[ "$a" = "$b" ]

In standard sh, pattern matching is done with case:

case $a in
  ($b) ...

For completeness, other equality-like operators you may come across in shell scripts:

  • [ "$a" -eq "$b" ]: standard [ operator to compare decimal integer numbers. Some [ implementations allow blanks around the numbers, some allow arbitrary arithmetic expressions, but that's not portable. Portably, one can use [ "$((a))" -eq "$((b))" ] for that. See also [ "$((a == b))" -ne 0 ] which would be the standard equivalent (except that POSIXly, the behaviour is only specified if $a and $b contain integer constants) of:
  • ((a == b)), from ksh and also found in zsh and bash, returns true if the evaluation of the arithmetic expression stored in $a yields the same number as that of $b. Typically, that's used for comparing numbers. Note that there are variations between shells as to how arithmetic expressions are evaluated and what numbers are supported (for instance bash and some implementation/versions of ksh don't support floating point or treat numbers with leading zeros as octal).

  • expr "$a" = "$b" does a number comparison if both operands are recognised as decimal integer numbers (some allowing blanks around the number), and otherwise checks if the two string operators have the same sorting order. It would also fail for values of $a or $b that are expr operators like (, substr...

  • awk 'BEGIN{exit !(ARGV[1] == ARGV[2])}' "$a" "$b": if $a and $b are recognised as numbers (at least decimal integer and floating point numbers like 1.2, -1.5e-4, leading trailing blanks ignored, some also recognising hexadecimal, octal or anything recognised by strtod()), then a numeric comparison is performed. Otherwise, depending on the implementation, it's either a byte-to-byte string comparison, or like for expr a strcoll() comparison, that is whether $a and $b sort the same.

See also:


These are equivalent in bash:

[[ $x == "$y" ]]
[[ $x = "$y" ]]
[ "$x" == "$y" ]
[ "$x" = "$y" ]

The first two $x variables don't have to be quoted. Bash performs word splitting and pathname expansion inside [ but not inside [[:

$ x='a b'
$ [ -s $x ]
-bash: [: a: binary operator expected
$ [[ -s $x ]]
$ ls
$ [ a = * ]
-bash: [: a: unary operator expected
$ [[ a = * ]]

[[ $x = "$y" ]] is a string comparison but [[ $x = $y ]] is a pattern matching expression:

$ y='a*'; [[ aa = "$y" ]]; echo $?
$ y='a*'; [[ aa = $y ]]; echo $?

-eq is only meant to be used with integers:

$ [[ x.x -eq x.x ]]
-bash: [[: x.x: syntax error: invalid arithmetic operator (error token is ".x")
$ x=9; [[ "x" -eq 9 ]]; echo $?

See also BashFAQ/031: What is the difference between test, [ and [[ ?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.