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

while

while checks a condition and, as long as it is true, runs its block. The condition is evaluated again before every iteration, so the block may run zero times.

See also: while command help and Control Flow - while in the Book.

mut x = 0
while $x < 10 { $x = $x + 1 }
$x
# => 10

Language Notes

  1. The condition is an expression that must evaluate to a bool. A pipeline or command call needs parentheses: while ($queue | is-empty) == false { ... }.

    mut queue = [a b c]
    while not ($queue | is-empty) {
      print $"processing ($queue | first)"
      $queue = ($queue | skip 1)
    }
    # => processing a
    # => processing b
    # => processing c
  2. A non-boolean condition is a runtime error; Nushell does not convert values to "truthy" or "falsy" ones.

    while 1 { break }
    # => Error: nu::shell::type_mismatch
    # =>
    # =>   × Type mismatch.
    # =>    ╭─[repl_entry #1:1:7]
    # =>  1 │ while 1 { break }
    # =>    ·       ┬
    # =>    ·       ╰── expected bool
    # =>    ╰────
  3. If the condition is false from the start, the block never runs:

    mut x = 10
    while $x < 3 { print "never runs" }
    $x
    # => 10
  4. Like loop, while evaluates to nothing, the block's values are discarded, and the block can update mut variables from the enclosing scope because it is a block and not a closure. For the same reason, the condition must not be written as a closure:

    mut x = 0; while { $x < 3 } { $x += 1 }
    # => Error: nu::parser::expected_keyword
    # =>
    # =>   × Capture of mutable variable.
    # =>    ╭─[repl_entry #1:1:20]
    # =>  1 │ mut x = 0; while { $x < 3 } { $x += 1 }
    # =>    ·                    ─┬
    # =>    ·                     ╰── capture of mutable variable
    # =>    ╰────
  5. break and continue work in while just as in loop and for. while true { ... } is equivalent to loop { ... }:

    mut i = 0
    while true {
      $i += 1
      if $i == 2 { continue }
      if $i > 4 { break }
      print $i
    }
    # => 1
    # => 3
    # => 4
  6. while does not accept pipeline input. To iterate over a list or table, use for or a filter such as each. To consume a list only while a condition holds, take while is usually simpler than a while loop:

    [1 3 5 6 7] | take while {|n| $n mod 2 == 1 }
    # => ╭───┬───╮
    # => │ 0 │ 1 │
    # => │ 1 │ 3 │
    # => │ 2 │ 5 │
    # => ╰───┴───╯
Edit this page on GitHub
Contributors: NotTheDr01ds, Kieron Wilkinson, fdncred
Prev
loop
Next
match