Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

powershell - Why does 'true' equal $true

I'm not quite sure if this exact question is a duplicate, but I couldn't find my specific question on stackoverflow, so I guess it's not.

$true is a boolean type:

($true).getType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Boolean                                  System.ValueType

'true' is a string:

('true').gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

why is this condition true?

PS C:WINDOWSsystem32> if ('True' -eq $true) {'yes'} else {'no'}
yes

only because the string is called True just like the boolean true? If I compare it with any other string, there is a different outcome:

PS C:WINDOWSsystem32> 'hello' -eq $true
False
PS C:WINDOWSsystem32> 'true' -eq $true
True

if you do 'true' == true in C# it prints an cannot convert string to bool error and both C# and PS are based on .net and have quite the same syntax. why is there a different outcome? this seems very strange to me.

How is PowerShell evaluating 'true' to $true?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In PowerShell, operator overloading is determined by the left-hand side (lhs) argument.

This means that when you supply a string as the lhs argument, the parser will attempt to convert the right-hand side (rhs) to a string as well.

When cast to a string, $true comes out as the string "True" - which, since PowerShell's string comparison operators are all case-insensitive by default, happens to equal "true".

This operation:

'true' -eq $true

Is interpreted as

'true' -eq 'True'

Which happens to be $true, satisfying your if condition.


With $true, this also happens to work the other way around, because a non-empty string, when converted to a boolean, evaluates to $true:

In other words

$true -eq 'true'

Is interpreted as

$true -eq $true

This can lead to confusion, since the string "false" will incidentally also be evaluated as $true, simply because the string "false" is not empty:

PS C:> $true -eq "true"
True
PS C:> $true -eq "false"
True
PS C:> $true -eq ""
False

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...