Ternary operator in PowerShell

后端 未结 13 2018
忘掉有多难
忘掉有多难 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"}) 
    
    0 讨论(0)
提交回复
热议问题