Is Nushell a Language?
Everything we've executed so far are commands that generate structured data and we pipe from one to another to manipulate this data. This isn't completely true because although they seem like commands, they're more than that, as they can act as instructions in a programming language.
What I mean is that we can write our own commands and reuse them within Nushell as functions in a language.
Let's create a simple command that expects an input value of how many replicas we want in deploy.yaml. Once we have the value, the deploy.yaml file should be opened and the spec.replicas value should be changed to the desired value and saved in the same file. We have only 1 replica defined in the file so far.
~> def get-replicas [] {
let num_replicas = (input "How many replicas do you want in deploy.yaml? ") | into int
open deploy.yaml
| upsert spec.replicas $num_replicas
| save deploy.yaml --force
$num_replicas
}
~> get-replicas
How many replicas do you want in deploy.yaml? 22
24
~> cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
replicas: 22
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
~>
A shell script executes all commands in order without checking the script first, that is, it executes commands in order. A Nu script is much smarter - it warns that there's an error before executing.
Let's prove it.
#!/bin/bash
echo "Starting Bash script"
# Tries to use an undefined variable
echo "The value of the variable is: $UNDEFINED_VARIABLE"
# Tries to access a directory that doesn't exist
cd /directory/that/doesnt/exist
echo "This line won't be executed if the directory doesn't exist"
Executing...
~> ./script.sh
Starting Bash script
The value of the variable is:
/Users/davidprata/./script.sh: line 6: cd: /directory/that/doesnt/exist: No such file or directory
This line won't be executed if the directory doesn't exist
The test with the Bash script confirms the behavior:
- The script started normally, printing "Starting Bash script".
- When trying to print the undefined variable, Bash simply printed an empty line, without generating an error.
- When it tried to change to a non-existent directory, Bash generated an error, but continued script execution.
- The last line was printed, even after the
cdcommand error.
This behavior demonstrates that Bash:
- Doesn't check the script in advance for possible errors.
- Executes commands sequentially, regardless of previous errors.
- Doesn't stop script execution when it encounters an error unless
set -eis used, which we didn't.
The problem is that it executed everything and would only catch the error along the way.
Now let's go to the same script written for Nushell (.nu).
#!/usr/bin/env nu
echo "Starting Nushell script"
# Tries to use an undefined variable
echo $"The value of the variable is: ($env.UNDEFINED_VARIABLE)"
# Tries to access a directory that doesn't exist
cd /directory/that/doesnt/exist
echo "This line won't be executed if the directory doesn't exist"
Trying to execute...
~> ./nuscript.nu
Error: nu::shell::column_not_found
Γ Cannot find column 'UNDEFINED_VARIABLE'
ββ[/Users/davidprata/nuscript.nu:6:34]
5 β # Tries to use an undefined variable
6 β echo $"The value of the variable is: ($env.UNDEFINED_VARIABLE)"
Β· ββββββββββββββ¬βββββββββββββ¬
Β· β β°ββ value originates here
Β· β°ββ cannot find column 'UNDEFINED_VARIABLE'
7 β
β°ββββ
~>
Nu Script already warned about the problem before even executing, that is, it didn't execute. Let's fix it and try again...
# defining the variable...
~> $env.UNDEFINED_VARIABLE = "any_value"
~> ./nuscript.nu
Error: nu::shell::directory_not_found
Γ Directory not found
ββ[/home/david/nuscript.nu:9:4]
8 β # Tries to access a directory that doesn't exist
9 β cd /directory/that/doesnt/exist
Β· βββββββββββββ¬ββββββββββββ
Β· β°ββ directory not found
10 β
β°ββββ
help: /directory/that/doesnt/exist does not exist
~>
Nushell isn't a language with a traditional compiler, but it does good static analysis.
To help create Nu scripts in the VSCode IDE, we have the extension vscode-nushell-lang.