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 that cannot be used to declare variables
          • 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

Range

Description:Describes a range of values from a starting value to an ending value, with an optional stride.
Annotation:range
Literal Syntax:<start_value>..<end_value> or <start_value>..<second_value>..<end_value>. E.g., 1..10.
Casts:seq
See also:Types of Data - Ranges

Additional Language Notes

  1. Ranges are inclusive by default.

    Examples:

    • Values from 1 to 5 inclusive:
    > 1..5
    ╭───┬────╮
    │ 0 │  1 │
    │ 1 │  2 │
    │ 2 │  3 │
    │ 3 │  4 │
    │ 4 │  5 │
    ╰───┴────╯
  2. In many programming languages, the step (or interval) is specified. Nushell's range is inspired by more functional languages, where the second value is literally the second value that should be generated. The step is then automatically calculated as the distance between the first and second values.

    Example - Values from 1 to 10, striding with 2 (only odds):

    > 1..3..10
    ╭───┬───╮
    │ 0 │ 1 │
    │ 1 │ 3 │
    │ 2 │ 5 │
    │ 3 │ 7 │
    │ 4 │ 9 │
    ╰───┴───╯
  3. Exclusive range - You can also use ..< to have values up to, but not including, the range end.

    Values from 1 to 5 (exclusive):

    > 1..<5
    ╭───┬───╮
    │ 0 │ 1 │
    │ 1 │ 2 │
    │ 2 │ 3 │
    │ 3 │ 4 │
    ╰───┴───╯
  4. Range values can be floats:

    (1.0)..(1.2)..(2.2)
    ╭───┬──────╮
    │ 0 │ 1.00 │
    │ 1 │ 1.20 │
    │ 2 │ 1.40 │
    │ 3 │ 1.60 │
    │ 4 │ 1.80 │
    │ 5 │ 2.00 │
    │ 6 │ 2.20 │
    ╰───┴──────╯

    Note: Parentheses (subexpressions) are not required in this example; they are simply used for readability.

  5. Ranges can also work backward:

    > 5..1
    ╭───┬───╮
    │ 0 │ 5 │
    │ 1 │ 4 │
    │ 2 │ 3 │
    │ 3 │ 2 │
    │ 4 │ 1 │
    ╰───┴───╯
  6. The start value is optional. The default start value is 0.

    > (..5) == (0..5)
    true
  7. The end value is also optional. The default end value is infinite:

    > 1..
    # => infinite range starting at 1

    Note: Interrupt the generation of the above range using Ctrl+C.

  8. Ranges are lazy, meaning they do not generate their values until needed. You can use a range with no specified end point and combine it with a command that takes only the first n elements. For example, you could generate the numbers from 1 to 10 using:

    > 1.. | take 5
    ╭───┬───╮
    │ 0 │ 1 │
    │ 1 │ 2 │
    │ 2 │ 3 │
    │ 3 │ 4 │
    │ 4 │ 5 │
    ╰───┴───╯
  9. Conversion - A range may be converted to a list using:

    1..5 | each {||}
Edit this page on GitHub
Contributors: NotTheDr01ds, fdncred, Callum Hart, yo-goto
Prev
Datetime
Next
String