Home Explore Blog CI



nushell

1st chunk of `book/thinking_in_nu.md`
cec83e6b53f7da5db09782f7133c71d3bbd8adf3d89970010000000100000fa1
# Thinking in Nu

Nushell is different! It's common (and expected!) for new users to have some existing "habits" or mental models coming from other shells or languages.

The most common questions from new users typically fall into one of the following topics:

[[toc]]

## Nushell isn't Bash

### It can sometimes look like Bash

Nushell is both a programming language and a shell. Because of this, it has its own way of working with files, directories, websites, and more. You'll find that some features in Nushell work similar to those you're familiar with in other shells. For instance, pipelines work by combining two (or more) commands together, just like in other shells.

For example, the following commandline works the same in both Bash and Nushell on Unix/Linux platforms:

```nu
curl -s https://api.github.com/repos/nushell/nushell/contributors | jq -c '.[] | {login,contributions}'
# => returns contributors to Nushell, ordered by number of contributions
```

Nushell has many other similarities with Bash (and other shells) and many commands in common.

::: tip
Bash is primarily a command interpreter which runs external commands. Nushell provides many of these as cross-platform, built-in commands.

While the above commandline works in both shells, in Nushell there's just no need to use the `curl` and `jq` commands. Instead, Nushell has a built-in [`http get` command](/commands/docs/http_get.md) and handles JSON data natively. For example:

```nu
http get https://api.github.com/repos/nushell/nushell/contributors | select login contributions
```

:::

::: warning Thinking in Nushell
Nushell borrows concepts from many shells and languages. You'll likely find many of Nushell's features familiar.
:::

### But it's not Bash

Because of this, however, it's sometimes easy to forget that some Bash (and POSIX in general) style constructs just won't work in Nushell. For instance, in Bash, it would be normal to write:

```sh
# Redirect using >
echo "hello" > output.txt
# But compare (greater-than) using the test command
test 4 -gt 7
echo $?
# => 1
```

In Nushell, however, the `>` is used as the greater-than operator for comparisons. This is more in line with modern programming expectations.

```nu
4 > 10
# => false
```

Since `>` is an operator, redirection to a file in Nushell is handled through a pipeline command that is dedicated to saving content - [`save`](/commands/docs/save.md):

```nu
"hello" | save output.txt
```

::: warning Thinking in Nushell
We've put together a list of common Bash'isms and how to accomplish those tasks in Nushell in the [Coming from Bash](./coming_from_bash.md) Chapter.
:::

## Implicit Return

Users coming from other shells will likely be very familiar with the `echo` command. Nushell's
[`echo`](/commands/docs/echo.md) might appear the same at first, but it is _very_ different.

First, notice how the following output _looks_ the same in both Bash and Nushell (and even PowerShell and Fish):

```nu
echo "Hello, World"
# => 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 [] {

Title: Thinking in Nu: Key Differences from Bash and Implicit Return
Summary
This section highlights key differences between Nushell and Bash, focusing on how Nushell, as a programming language and shell, handles file operations, comparisons, and command outputs differently. It introduces the concept of implicit return, where Nushell automatically returns the value of an expression, distinguishing it from other shells like Bash that rely on standard output.