Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is $false -eq "" true?

Tags:

powershell

The following code segments output true:

$x = ($false -eq "") 
Write-Host $x

$x = ($false -eq 0) 
Write-Host $x

Since $false and "" are different data types, shouldn't it automatically equal false?

like image 892
David Klempfner Avatar asked Sep 24 '14 10:09

David Klempfner


People also ask

Why does false implies true true?

So the reason for the convention 'false implies true is true' is that it makes statements like x<10→x<100 true for all values of x, as one would expect. Very well done, this is actually the most convincing explanation I've seen for this law.

Do two Falses make a true?

No. Formal logic (valid reasoning) guarantees only that from true statements, a false conclusion (statement) cannot be deduced. Valid reasoning preserves the truth of premises. It does not say that from false premisses a true conclusion cannot be reached (by a multiple logically invalid steps).

Why is 0 false and 1 true?

Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.

What does false || true return?

The true operator returns the bool value true to indicate that its operand is definitely true. The false operator returns the bool value true to indicate that its operand is definitely false.


2 Answers

When doing comparison operations, PowerShell will automatically attempt to coerce the object on the right-hand side of the operator to match the type on the left-hand side.

In the case of coercing [string] to [bool], any non-null string will evaluate as $true, and a null string will evaluate as $false. See blog post Boolean Values and Operators for more information about automatic conversion of different data types to boolean values.

This sometimes leads to unexpected results:

PS C:\> [bool]"$false" 

True

The string value of $false is 'False', which is a non-null string and evaluated to $true when cast back to [bool].

It also makes comparison operations non-commutative when the operands are of different data types:

PS C:\> '' -eq $false
False
PS C:\> $false -eq ''
True

In the first comparison the value $false is auto-cast to a string in order to match the type of the first operand (''), so you're actually comparing '' -eq 'False', which evaluates to $false.

In the second comparison the string '' is auto-cast to a boolean, again in order to match the type of the first operand ($false), so this time you're actually comparing $false -eq $false, which evaluates to $true.

like image 106
mjolinor Avatar answered Nov 09 '22 06:11

mjolinor


Just sharing one experience here which might be worth noting when we convert a string value to boolean:

What I was doing is reading a boolean string value from a configuration file which was getting stored in a variable as shown below:

$valueReadFromFile = "false"

Now, I wanted to convert it to Boolean value. Since I was not aware of Convert class in PowerShell I used casting instead as shown below in a bool condition of if block:

if([bool]$valueReadFromFile -eq $true)
{
    echo "This message shouldn't get printed in current scenario"
}

But I was on a wrong turn. Everytime below message was getting printed because non-empty string in PowerShell gets casted to literal boolean value $true:

This message shouldn't get printed in current scenario

When my program started to behave incorrectly then I explored more and came to know about convert class. I fixed my code as below:

$actualBoolValue = [System.Convert]::ToBoolean($valueReadFromFile.Trim())
like image 41
RBT Avatar answered Nov 09 '22 07:11

RBT