Haotian Chyi
Haotian Chyi

Reputation: 75

Why shell test with -o seems return a wrong result?

I am a newbie. I have read the man doc of test, and it say

EXPRESSION1 -o EXPRESSION2

either EXPRESSION1 or EXPRESSION2 is true

I run this in my shell:

[ false -o false ] && echo "what happened?"

and it print the string, why? :(

Upvotes: 2

Views: 34

Answers (2)

KamilCuk
KamilCuk

Reputation: 141523

From man test:

-n STRING
the length of STRING is nonzero

STRING
equivalent to -n STRING

The false is interpreted as a string. So [ false -o false ] is [ -n false -o -n false ]. As the string false has non-zero length (has 5 characters) the expression is true.

Upvotes: 2

l0b0
l0b0

Reputation: 58918

false in a test statement is just a string, and non-empty strings are truthy. Conversely:

$ false && false && echo 'Nope'
$ echo $?
1

Upvotes: 2

Related Questions