Home Explore Blog CI



nushell

8th chunk of `blog/2022-03-22-nushell_0_60.md`
20b1d34b115f437e7f629387b17b47ad6cedd719d22890a800000001000010ee
To redirect stderr, you can call through `do -i`. For example, let's say we're calling `cat` again, this time with a file that doesn't exist:

```
> do -i { cat unknown.txt } | complete
╭───────────┬─────────────────────────────────────────────╮
│ stdout    │                                             │
│ stderr    │ cat: unknown.txt: No such file or directory │
│ exit_code │ 1                                           │
╰───────────┴─────────────────────────────────────────────╯
```

You can also access the last exit code via `$env.LAST_EXIT_CODE`.

## Modules

With 0.60, you're now able to create your own modules, allowing you to grow to larger projects with clean interfaces between files. A module can be written either using the `module` keyword:

```
module greetings {
  export def greet [] {
    print "hello!"
  }
}

use greetings greet
greet
```

You can also make modules from whole files. We can rewrite the above using a separate file:

```
# greetings.nu
export def greet [] {
  print "hello!"
}

# main.nu
use greetings.nu greet
greet
```

Modules allow you to `export` custom commands and environment variables to be used in other places.

## Make your own errors

You can also create your own error messages in your custom commands and send these back to the user if they call your command in the wrong way. For example, let's say you wanted to require that the user pass in a value between 1 and 10:

```
> def one-to-ten [x: int] {
    let span = (metadata $x).span
    if $x > 10 || $x < 1 {
      error make {
        msg: "Value not between 1 and 10",
        label: {
          text: "expected a value between 1 and 10",
          start: $span.start,
          end: $span.end
        }
      }
    } else {
      $"passed ($x)"
    }
  }
> one-to-ten 6
passed 6
> one-to-ten 21
Error:
  × Value not between 1 and 10
   ╭─[entry #47:1:1]
 1 │ one-to-ten 21
   ·            ─┬
   ·             ╰── expected a value between 1 and 10
   ╰────
```

## New commands

To accompany the new functionality in the language, we've also added a number of new commands.

| category    | command                                                                                                                                                          |
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| conversions | `into bool`, `into datetime`, `into decimal`, `into duration`                                                                                                    |
| core        | `def-env`, `error make`, `export def env`, `export def`, `export env`, `export`, `env`, `extern`, `hide`, `metadata`, `module`, `register`, `use`, `view-source` |
| filters     | `columns`, `group`, `par-each`, `transpose`, `window`, `roll`, `roll down`, `roll left`, `roll right`, `upsert`                                                  |
| formats     | `from nuon`, `to nuon`                                                                                                                                           |
| plugins     | `gstat`                                                                                                                                                          |
| strings     | `decode`, `nu-highlight`, `print`, `fmt`                                                                                                                         |
| system      | `complete`, `input`, `keybindings`, `keybindings default`, `keybindings list`, `keybindings listen`                                                              |
| viewers     | `grid`                                                                                                                                                           |

# Shell improvements

## Bang bang and more

You can now use `!!` to run the previous command, or `!` followed by the row number in the `history` you'd like to run again.

Title: Nushell 0.60: Modules, Custom Errors, and New Commands
Summary
This section explains how to create and use modules in Nushell, allowing for the export of custom commands and environment variables. It details how to create custom error messages within commands. The update introduces several new commands across different categories, including conversions, core functionalities, filters, formats, plugins, strings, system utilities, and viewers.