W3cubDocs

/Nushell

if for core

Conditionally run a block.

Signature

> if {flags} (cond) (then_block) (else_expression)

Parameters

  • cond: condition to check
  • then_block: block to run if check succeeds
  • else_expression: expression or block to run if check fails

Input/output types:

input output
any any

Examples

Output a value if a condition matches, otherwise return nothing

>if2<3 { 'yes!' }
yes!

Output a value if a condition matches, else return another value

>if5<3 { 'yes!' } else { 'no!' }
no!

Chain multiple if's together

>if5<3 { 'yes!' } else if4<5 { 'no!' } else { 'okay!' }
no!

Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/if.html