exbuddha
exbuddha

Reputation: 633

Bash: Why does [[ zero -eq 0 ]] evaluate to true?

On Mac and Ubuntu, running this command evaluates to true. Why?

[[ zero -eq 0 ]] && echo true
true

Upvotes: 2

Views: 782

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295618

-eq runs a numeric comparison.

When a string is given as an operand, the value of a like-named variable is looked up.

Thus, this becomes equivalent to [[ $zero -eq 0 ]]. An empty string has a numeric value of 0. Thus, unless a different value has been assigned to the shell variable named zero, this is equivalent to [[ 0 -eq 0 ]], which is true.

Upvotes: 6

Related Questions