Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Language Reference Guide
    • Readme
    • Types in the Nu Language
      • Basic Types
        • Any
        • Boolean
        • Integer
        • Float
        • Filesize
        • Duration
        • Datetime
        • Range
        • String
        • Record
        • List
        • Table
        • Closure
        • Nothing
        • Binary
        • Glob
        • Cell-Path
      • Other Data Types

        • Types used only in command signatures
          • Path
        • Types which are not declarable
          • Error
          • CustomValue
          • Block
      • Type signatures
      • Commands that interact with types
    • Operators
    • Flow control
      • if/else
      • loop
      • while
      • match
      • try/catch
      • break
      • return
      • continue
    • Filters
      • each and par-each
      • Filters to select subsets of data
      • where and filter
      • Understanding the difference between get and select
    • Custom Commands
    • Declarations
    • Variable Scope
    • Strings and Text Formatting
    • Helpers and debugging commands
    • Pipelines
    • MIME Types for Nushell

if/else

The if expression evaluates a condition and then chooses to run a block based on the condition.

For example, you can print "yes", based on a true condition:

if true {
  print yes
} else {
  print no
}
# => yes

Alternately, you can print "no", based on a false condition:

if false {
  print yes
} else {
  print no
}
# => no

The else part of if is optional. If not provided, if a condition is false, the if expression returns null.

The code that follows the else is an expression rather than a block, allowing any number of follow-on if expressions as well as other types of expressions. For example, this expression returns 100: if false { 1 } else 100.

Chained else if expressions are checked in order, and the first block whose condition is true is run:

let x = 5
if $x > 10 { "big" } else if $x > 3 { "medium" } else { "small" }
# => medium

Because if is an expression, its result can be assigned to a variable. The value is the value of the last expression in the block that ran:

let x = 5
let label = if $x mod 2 == 0 { "even" } else { "odd" }
$label
# => odd

The condition must be a bool. Nushell does not treat other values such as 1, 0, or an empty string as true or false:

if 1 { "yes" }
# => Error: nu::shell::cant_convert
# =>
# =>   × Can't convert to boolean.
# =>    ╭─[repl_entry #1:1:4]
# =>  1 │ if 1 { "yes" }
# =>    ·    ┬
# =>    ·    ╰── can't convert int to boolean
# =>    ╰────

See also: match for choosing between many values.

Edit this page on GitHub
Contributors: NotTheDr01ds, fdncred
Next
loop