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.
#!/bin/bash
a="hello"
b="world"
if [[ $a == $b ]];then
echo "equal"
fi
Is there a difference between =
and ==
in the comparison?
Best Answer
[[ $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 byksh
).[[...]]
is not standardsh
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, variable expansions must be quoted to prevent split+glob and empty removal (only the latter being performed in
zsh
), so:In standard
sh
, pattern matching is done withcase
: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 inzsh
andbash
, 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 areexpr
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 bystrtod()
), then a numeric comparison is performed. Otherwise, depending on the implementation, it's either a byte-to-byte string comparison, or like forexpr
astrcoll()
comparison, that is whether$a
and$b
sort the same.See also: