2

I have very dumb problem but can't wrap my head around it

if [[ false || false ]] ; then
        echo 'true'
else
        echo 'false'
fi

As per http://tldp.org/LDP/abs/html/comparison-ops.html

-o logical or

exp1 -o exp2 returns true if either exp1 or exp2 is true.

These are similar to the Bash comparison operators && and ||, used within double brackets. [[ condition1 && condition2 ]]

so if both are false then it should return false? then why it prints 'true'?

2 Answers 2

4

You should run those not as part of the conditional command '[[ ]]':

if false || false; then
        echo 'true'
else
        echo 'false'
fi

As for testing falses within [[ and ]]:

if [[ ! 1 || ! 1 ]]; then
        echo 'true'
else
        echo 'false'
fi

Noting that [[ false ]] is equivalent to [[ -n false ]] which makes a true condition.

If you like you could make a more apparent and valid conditional test with (( )) like this:

if (( 0 || 0 )); then
        echo 'true'
else
        echo 'false'
fi
Sign up to request clarification or add additional context in comments.

1 Comment

"" (the empty string) is probably a more canonical false value in bash; note that ! 0 is just as false in this context as ! 1.
4

"false" is not false. "false" is a non-empty string. Non-empty strings are true by default in [[.

1 Comment

"false" is not a keyword in [[.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.