Scripts

In Nushell, you can write and run scripts in the Nushell language. To run a script, you can pass it as an argument to the nu commandline application:

  1. > nu myscript.nu

This will run the script to completion in a new instance of Nu. You can also run scripts inside the current instance of Nu using source:

  1. > source myscript.nu

Let’s look at an example script file:

  1. # myscript.nu
  2. def greet [name] {
  3. echo "hello" $name
  4. }
  5. greet "world"

A script file defines the definitions for custom commands as well as the main script itself, which will run after the custom commands are defined.

In the above, first greet is defined by the Nushell interpreter. This allows us to later call this definition. We could have written the above as:

  1. greet "world"
  2. def greet [name] {
  3. echo "hello" $name
  4. }

There is no requirement that definitions have to come before the parts of the script that call the definitions, allowing you to put them where you feel comfortable.

How scripts are processed

In a script, definitions run first. This allows us to call the definitions using the calls in the script.

After the definitions run, we start at the top of the script file and run each group of commands one after another.

Script lines

To better understand how Nushell sees lines of code, let’s take a look at an example script:

  1. a
  2. b; c | d

When this script is run, Nushell will first run the a command to completion and view its results. Next, Nushell will run b; c | d following the rules in the “Groups” section.

Parameterizing Scripts

Script files can optionally contain a special “main” command. main will be run after any other Nu code, and is primarily used to add parameters to scripts. You can pass arguments to scripts after the script name (nu <script name> <script args>).

For example:

  1. # myscript.nu
  2. def main [x: int] {
  3. $x + 10
  4. }
  1. > nu myscript.nu 100
  2. 110

Shebangs (#!)

On Linux and macOS you can optionally use a shebangScripts - 图1 (opens new window)) to tell the OS that a file should be interpreted by Nu. For example, with the following in a file named myscript:

  1. #!/usr/bin/env nu
  2. echo "Hello World!"
  1. > ./myscript
  2. Hello World!