Scripts
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:
> source myscript.nu
Let’s look at an example script file:
# myscript.nu
echo "hello" $name
}
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.
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.
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.
a
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 .
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:
$x + 10
}
#!/usr/bin/env nu
echo "Hello World!"
Hello World!