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

Viewed 30

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? :(

2 Answers

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

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

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.

Related