Powershell - What does if($variable) test for?

前端 未结 2 1571
一向
一向 2021-01-19 15:42

In Powershell, what does if($variable) test for? Is this testing if the variable is set, is null, is true, or something else?

2条回答
  •  庸人自扰
    2021-01-19 16:43

    It tests whether the variable is true or whether a non-Boolean variable can be coalesced to true. For example, each of these return false:

    $var #uninitialized
    $var = ""
    $var = $false
    $var = 0
    

    Each of these return true:

    $var = "something"
    $var = $true
    $var = 1 #or any non-zero number
    

提交回复
热议问题