Ternary operator in PowerShell

后端 未结 13 2110
忘掉有多难
忘掉有多难 2020-11-28 03:56

From what I know, PowerShell doesn\'t seem to have a built-in expression for the so-called ternary operator.

For example, in the C language, which supports the terna

13条回答
  •  臣服心动
    2020-11-28 04:36

    $result = If ($condition) {"true"} Else {"false"}
    

    Everything else is incidental complexity and thus to be avoided.

    For use in or as an expression, not just an assignment, wrap it in $(), thus:

    write-host  $(If ($condition) {"true"} Else {"false"}) 
    

提交回复
热议问题