The difference between $ true and $ false under the hood

So, I played a little with PowerShell and came across a somewhat unexpected result in the following code

function returns_true(){ return $true } function returns_false(){ return $false } if (returns_true -eq $true){ Write-Host "I return true" } if (returns_false -eq $false){ Write-Host "I return false" } $myval = returns_false Write-Host $myval 

Running this gives me the following output

 I return true False 

I expected this to return either

 I return true I return false False 

or simply

 False 

Can someone explain what is happening here. What does PowerAhell do under the hood, which allows me to evaluate -eq $true , but not -eq $false , when I return $true and $false ?

+5
source share
2 answers

What actually happens here is that the parser sees -eq $ true and -eq $ false as arguments passed to these functions.

To verify that this is the case, add [CmdletBinding ()] Param () to the following functions:

 function returns_true(){ [CmdletBinding()] Param() return $true } 
+5
source

The best explanation of what is going on: fooobar.com/questions/1275713 / ...

Brackets help. Do you really want:

 if ((returns_true) -eq $true){ 

and

 if ((returns_false) -eq $false){ 
+3
source

Source: https://habr.com/ru/post/1275713/


All Articles