Home Explore Blog CI



nushell

2nd chunk of `book/thinking_in_nu.md`
853eb8aa9fe393629f1200aa78702c02bf47a09e2b3fc1360000000100000fc5
# => Hello, World
```

But while the other shells are sending `Hello, World` straight to _standard output_, Nushell's `echo` is
simply _returning a value_. Nushell then _renders_ the return value of a command, or more technically, an _expression_.

More importantly, Nushell _implicitly returns_ the value of an expression. This is similar to PowerShell or Rust in many respects.

::: tip
An expression can be more than just a pipeline. Even custom commands (similar to functions in many languages, but we'll cover them more in depth in a [later chapter](./custom_commands.md)) automatically, implicitly _return_ the last value. There's no need for an `echo` or even a [`return` command](/commands/docs/return.md) to return a value - It just _happens_.
:::

In other words, the string _"Hello, World"_ and the output value from `echo "Hello, World"` are equivalent:

```nu
"Hello, World" == (echo "Hello, World")
# => true
```

Here's another example with a custom command definition:

```nu
def latest-file [] {
    ls | sort-by modified | last
}
```

The _output_ of that pipeline (its _"value"_) becomes the _return value_ of the `latest-file` custom command.

::: warning Thinking in Nushell
Most anywhere you might write `echo <something>`, in Nushell, you can just write `<something>` instead.
:::

## Single Return Value per Expression

It's important to understand that an expression can only return a single value. If there are multiple subexpressions inside an expression, only the **_last_** value is returned.

A common mistake is to write a custom command definition like this:

```nu:line-numbers
def latest-file [] {
    echo "Returning the last file"
    ls | sort-by modified | last
}

latest-file
```

New users might expect:

- Line 2 to output _"Returning the last file"_
- Line 3 to return/output the file

However, remember that `echo` **_returns a value_**. Since only the last value is returned, the Line 2 _value_ is discarded. Only the file will be returned by line 3.

To make sure the first line is _displayed_, use the [`print` command](/commands/docs/print.md):

```nu
def latest-file [] {
    print "Returning last file"
    ls | sort-by modified | last
}
```

Also compare:

```nu
40; 50; 60
```

::: tip
A semicolon is the same as a newline in a Nushell expression. The above is the same as a file or multi-line command:

```nu
40
50
60
```

or

```nu
echo 40
echo 50
echo 60
```

See Also: [Multi-line Editing](./line_editor.md#multi-line-editing)
:::

In all of the above:

- The first value is evaluated as the integer 40 but is not returned
- The second value is evaluated as the integer 50 but is not returned
- The third value is evaluated as the integer 60, and since it is the last
  value, it is is returned and displayed (rendered).

::: warning Thinking in Nushell
When debugging unexpected results, be on the lookout for:

- Subexpressions (e.g., commands or pipelines) that ...
- ... output a (non-`null`) value ...
- ... where that value isn't returned from the parent expression.

These can be likely sources of issues in your code.
:::

## Every Command Returns a Value

Some languages have the concept of "statements" which don't return values. Nushell does not.

In Nushell, **_every command returns a value_**, even if that value is `null` (the `nothing` type). Consider the following multiline expression:

```nu:line-numbers
let p = 7
print $p
$p * 6
```

1. Line 1: The integer 7 is assigned to `$p`, but the return value of the
   [`let` command](/commands/docs/let.md) itself is `null`. However, because it is not the last
   value in the expression, it is not displayed.
2. Line 2: The return value of the `print` command itself is `null`, but the `print` command
   forces its argument (`$p`, which is 7) to be _displayed_. As with Line 1, the `null` return value
   is discarded since this isn't the last value in the expression.
3. Line 3: Evaluates to the integer value 42. As the last value in the expression, this is the return
   result, and is also displayed (rendered).

Title: Nushell: Single Return Value per Expression and Every Command Returns a Value
Summary
This section delves into Nushell's expression evaluation, emphasizing that only the last value in an expression is returned. It highlights a common mistake where users expect multiple outputs from a custom command but only the last expression's value is returned, unless the `print` command is used. Additionally, it clarifies that every command in Nushell returns a value, even if it's `null`, and illustrates how this impacts the execution and output of multiline expressions.