W3cubDocs

/Nushell

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:

input output
any any

Examples

Match on a value in range

>match3 { 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

>match3 { 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 [123] {
        [$x, ..$y] if$x==1=> { 'good list' },
_=> { 'not a very good list' }
    }

goodlist

Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/match.html