It depends on the context. In a math context (which is preferable if you're writing your scripts specifically for bash), use ==
.
(( foo == 3 )) ## foo = 3 would be an assignment here, and foo -eq 3 an error
A math context is also present in other situations -- for instance, indexing into a non-associative array, making ==
preferred but -eq
illegal in the below contrieved example:
foo=( yes no )
bar=3
echo "${foo[bar == 3 ? 0 : 1]}" # echoes "yes"
In [[ ]]
, use -eq
.
[[ $foo -eq 3 ]] ## $foo = 3 would be a string comparison here; $foo == 3 is a synonym,
## but bad finger-memory to have if one wants to write POSIX code
## elsewhere.
In [ ]
, use -eq
-- and also quote:
[ "$foo" -eq 3 ] ## = would be a valid string comparison here; == would be a
## POSIX-incompatible string comparison here; -eq is correct.