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
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

match for core

Conditionally run a block on a matched value.

Signature

> match {flags} (value) (match_block)

Parameters

  • value: Value to check.
  • match_block: Block to run if check succeeds.

Input/output types:

inputoutput
anyany

Examples

Match on a value

> match 3 { 1 => 'one', 2 => 'two', 3 => 'three' }
three

Match against alternative values

> match 'three' { 1 | 'one' => '-', 2 | 'two' => '--', 3 | 'three' => '---' }
---

Match on a value in range

> match 3 { 1..10 => 'yes!' }
yes!

Match on a field in a record

> match {a: 100} { {a: $my_value} => { $my_value } }
100

Match with a catch-all

> match 3 { 1 => { 'yes!' }, _ => { 'no!' } }
no!

Match against a list

> match [1, 2, 3] { [$a, $b, $c] => { $a + $b + $c }, _ => 0 }
6

Match against pipeline input

> {a: {b: 3}} | match $in {{a: { $b }} => ($b + 10) }
13

Match with a guard

> match [1 2 3] {
        [$x, ..$y] if $x == 1 => { 'good list' },
        _ => { 'not a very good list' }
    }

good list

Notes

This command is a parser keyword. For details, check: https://www.nushell.sh/book/thinking_in_nu.html