Both true
and false
are, in your case, just strings, which evaluate to the logical true.
So regardless on whether state contains the word true
or false
, [ ! $state ]
will always revert to the logical false
-- triggering your else
clause.
Since you are already using true
and false
, there is something you can do: Both true
and false
are valid shell commands that do nothing but return an error code of 0 and 1, respectively. Since if
basically evaluates a command (look it up, [
is an executable) and uses their error code, you could do something like this:
if $state;
$state
would be either false
or true
, which would then be executed by if
, and, depending on the error code, select which specific code block of the if ... else ... fi
block would be executed.