nushell
Intro
You've mastered bash.
You are piping from one command to another effortless.
You use `cut`, `tr`, `sed`, `awk`, `grep` etc to transform data from one
command to the next.
You are starting to wonder.
What if you didn't have to transform data?
What if the data had a little more structure?
What if commands understood each other?
Presenting nushell. It is a reimagining of a shell, that works with structured data.
Install
Ubuntu/Debian
wget -qO- https://apt.fury.io/nushell/gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/fury-nushell.gpg
echo "deb [signed-by=/etc/apt/keyrings/fury-nushell.gpg] https://apt.fury.io/nushell/ /" | sudo tee /etc/apt/sources.list.d/fury.list
sudo apt update
sudo apt install nushell
Arch Linux
sudo pacman -Sy nushell
First steps
After installation, you can start a nushell session by typing nu in your
existing shell.
As a quick demo, let’s try and see what processes take up a lot (more than 500mb) of memory on our system.
ps | sort-by mem | where mem > 500mb
If we try the same in bash, we might end up with something like this.
ps -eo pid,rss,comm --sort=-rss | awk '$2 > 512000 { print }'
Notice how the nushell version outputs in a neatly formatted table.
Formats
When nushell understands the structure of the data, it is able to output in a nice table.
Here is an example with JSON:
http get https://v2.jokeapi.dev/joke/Programming?type=single
We can easily extract a field.
http get https://v2.jokeapi.dev/joke/Programming?type=single | get joke
Or multiple.
http get https://v2.jokeapi.dev/joke/Programming?type=twopart | select setup delivery
It can also output in various formats.
http get https://v2.jokeapi.dev/joke/Programming?type=single | to yaml
See loading data.
Functions
The syntax for functions is quite different from bash. A nice thing is that parameters have a name.
def hello [name] {
$"Hello ($name)"
}
hello world
See custom functions.
Variables
They can be declared as immutable (can’t be reassigned) with let and mutable
(can be reassigned) with mut.
let name = "Bob"
$name = "Alice"
Where line 2 will error.
mut name = "Bob"
$name = "Alice"
Control structures
If-else
Like most programming/scripting languages it supports if-else.
if $x > 0 { 'positive' } else if $x == 0 { 'zero' } else { "negative" }
Match
There are also match which resembles case in bash.
match 7 {
1 => 'one',
2 => 'two',
3 => 'three',
_ => 'other number'
}
Loops
There are a couple of different loops.
For-in
for $item in [1 2 3] { print $item }
Each
[1 2 3] | each { print $in }
While
mut x = 0
while $x < 10 { $x = $x + 1 }
$x
Outro
This is just scratching the surface. The Nu language can do much more than shown here. If I were to cover it all, that would be a whole new Christmas calendar.
My first impression of Nu scripting language is that it feels a lot more modern and consistent than bash. It got features found in other popular languages, mixing ideas from imperative and functional programming styles.