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

default for filters

Sets a default value if a row's column is missing or null.

Signature

> default {flags} (default value) ...rest

Flags

  • --empty, -e: also replace empty items like "", {}, and []

Parameters

  • default value: The value to use as a default.
  • ...rest: The name of the column.

Input/output types:

inputoutput
anyany

Examples

Give a default 'target' column to all file entries

> ls -la | default 'nothing' target

Get the env value of MY_ENV with a default value 'abc' if not present

> $env | get --ignore-errors MY_ENV | default 'abc'
abc

Replace the null value in a list

> [1, 2, null, 4] | each { default 3 }
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
│ 3 │ 4 │
╰───┴───╯

Replace the missing value in the "a" column of a list

> [{a:1 b:2} {b:1}] | default 'N/A' a
╭───┬─────┬───╮
│ # │  a  │ b │
├───┼─────┼───┤
│ 0 │   1 │ 2 │
│ 1 │ N/A │ 1 │
╰───┴─────┴───╯

Replace the empty string in the "a" column of a list

> [{a:1 b:2} {a:'' b:1}] | default -e 'N/A' a
╭───┬─────┬───╮
│ # │  a  │ b │
├───┼─────┼───┤
│ 0 │   1 │ 2 │
│ 1 │ N/A │ 1 │
╰───┴─────┴───╯

Generate a default value from a closure

> null | default { 1 + 2 }
3

Fill missing column values based on other columns

> [{a:1 b:2} {b:1}] | upsert a {|rc| default { $rc.b + 1 } }
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 2 │
│ 1 │ 2 │ 1 │
╰───┴───┴───╯