Home Explore Blog CI



nushell

1st chunk of `lang-guide/chapters/types/basic_types/nothing.md`
9f04f90cefe5f26eb2a9bac50a31c58868547cbf2706525f0000000100000c65
# Nothing

|                       |                                                                             |
| --------------------- | --------------------------------------------------------------------------- |
| **_Description:_**    | The `nothing` type is to be used to represent the absence of another value. |
| **_Annotation:_**     | `nothing`                                                                   |
| **_Literal Syntax:_** | `null`                                                                      |
| **_Casts:_**          | `ignore`                                                                    |
| **_See also:_**       | [Types of Data - Nothing](/book/types_of_data.md#nothing-null)              |

## Additional Language Notes

1. Recommended for use as a missing value indicator.
1. Commands that explicitly do not return a value (such as `print` or `if`) return `null`.
1. Commands that do not accept pipeline input have an input signature of `nothing`.

1. `null` is similar to JSON's "null". However, whenever Nushell would print the `null` value (outside of a string or data structure), it prints nothing instead.

   Example:

   ```nu
   > null | to json
   null
   > "null" | from json
   # => no output
   ```

1. You can add `ignore` at the end of a pipeline to convert any pipeline result to a `nothing`. This will prevent the command/pipeline's output from being displayed.

   ```nu
   git checkout featurebranch | ignore
   ```

1. It's important to understand that `null` is not the same as the absence of a value! It is possible for a table or list to have _missing_ values. Missing values are displayed with a ❎ character in interactive output.

   ```nu
   > let missing_value = [{a:1 b:2} {b:1}]
   > $missing_value
   ╭───┬────┬───╮
   │ # │ a  │ b │
   ├───┼────┼───┤
   │ 0 │  1 │ 2 │
   │ 1 │ ❎ │ 1 │
   ╰───┴────┴───╯
   ```

1. By default, attempting to access a missing value will not produce `null` but will instead generate an error:

   ```nu
   > let missing_value = [{a:1 b:2} {b:1}]
   > $missing_value.1.a
   Error: nu::shell::column_not_found

     × Cannot find column 'a'
     ╭─[entry #4:1:32]
   1 │ let missing_value = [{a:1 b:2} {b:1}]
     ·                                ──┬──
     ·                                  ╰── value originates here
     ╰────
     ╭─[entry #6:1:18]
   1 │ $missing_value.1.a
     ·                  ┬
     ·                  ╰── cannot find column 'a'
     ╰────
   ```

1. To safely access a value that may be missing, mark the cell-path member as _optional_ using a question-mark (`?`) after the key name.
   See [Navigating and Accessing Structured Data - Handling Missing Data](/book/navigating_structured_data.html#handling-missing-data) for more details and examples.

## Related commands

- [`default`](/commands/docs/default.html): Set a default value for missing (`null`) fields in a record or table
- [`compact`](/commands/docs/compact.html): Removes `null` values from a list

Title: Nushell: The 'nothing' Type
Summary
This section describes the 'nothing' type in Nushell, which represents the absence of a value. It is denoted by `null` and has specific behaviors, such as being used as a missing value indicator, returned by commands with no output, and serving as the input signature for commands that don't accept pipeline input. It also highlights the difference between `null` and missing values, and how to handle potentially missing values using optional cell-path members. Furthermore, it mentions related commands like `default` and `compact` that interact with `null` values.