Why does adding spaces around bash comparison operator change the result?

bash

Solution

"1==2" is a single 4-character string, not an expression involving the `==` operator. Non-empty strings always evaluate to true in the context of the conditional expression `[[ ... ]]`. Whitespace is mandatory around the `==` operator.

Like everything else in `bash`, the contents of `[[ ... ]]` are simply a white-space-separated list of arguments. The `bash` grammar doesn't know how to parse conditional expressions, but it does know how to interpret a list of 3 arguments like `1`, `==`, and `2` in the context of the `[[ ... ]]` compound command.

Problem

Could someone explain why spaces around `==` change the comparison result? The following: ``` if [[ 1 == 2 ]] ; then echo ok ; fi ``` prints nothing, while ``` if [[ 1==2 ]] ; then echo ok ; fi ``` prints `ok`

Original source