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

break

break stops the innermost enclosing loop immediately. Execution continues with the first statement after that loop.

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

for x in 1..10 { if $x > 3 { break }; print $x }
# => 1
# => 2
# => 3

Language Notes

  1. break works only inside for, while and loop. Using it anywhere else is a compile error. This includes closures such as the ones passed to each, where or do, even when that closure is itself inside a loop, because a closure is a separate function:

    [1 2 3] | each {|x| if $x == 2 { break }; $x }
    # => Error: nu::compile::not_in_a_loop
    # =>
    # =>   × 'break' can only be used inside a loop
    # =>    ╭─[repl_entry #1:1:34]
    # =>  1 │ [1 2 3] | each {|x| if $x == 2 { break }; $x }
    # =>    ·                                  ──┬──
    # =>    ·                                    ╰── can't be used outside of a loop
    # =>    ╰────
    for x in 1..3 { do { break } }
    # => Error: nu::compile::not_in_a_loop
    # =>
    # =>   × 'break' can only be used inside a loop
    # =>    ╭─[repl_entry #1:1:22]
    # =>  1 │ for x in 1..3 { do { break } }
    # =>    ·                      ──┬──
    # =>    ·                        ╰── can't be used outside of a loop
    # =>    ╰────

    Blocks that are not closures, such as the branches of if and match and the body of try, are fine.

  2. To stop a stream early without a loop, use take while or take until instead:

    [1 2 3 4 5 6] | take while {|x| $x <= 3 }
    # => ╭───┬───╮
    # => │ 0 │ 1 │
    # => │ 1 │ 2 │
    # => │ 2 │ 3 │
    # => ╰───┴───╯
  3. break takes no arguments. Loops always evaluate to nothing, so there is no value to pass out. Assign to a mut variable before breaking, or use return inside a custom command.

    for x in [1 2] { break 5 }
    # => Error: nu::parser::extra_positional
    # =>
    # =>   × Extra positional argument.
    # =>    ╭─[repl_entry #1:1:24]
    # =>  1 │ for x in [1 2] { break 5 }
    # =>    ·                        ┬
    # =>    ·                        ╰── extra positional argument
    # =>    ╰────
    # =>   help: Usage: break
  4. In nested loops, break leaves only the innermost loop. There are no loop labels:

    for i in 1..3 { for j in 1..3 { if $j == 2 { break }; print $"($i),($j)" } }
    # => 1,1
    # => 2,1
    # => 3,1
  5. A finally block of a surrounding try still runs when break jumps out of it:

    for i in 1..3 { try { break } finally { print "cleanup" } }; print done
    # => cleanup
    # => done
Edit this page on GitHub
Contributors: NotTheDr01ds, Kieron Wilkinson, fdncred
Prev
try/catch
Next
return