Nushell

Updated: 06 December 2023

Nushell Docs

About

Nushell makes use of command outputs as data that can be transformed, it makes use of pipes that connnect commands together in a functional-programming usage style

Thinking in Nu

Nushell works with results using pipes, this is similar to > in bash but isn’t exactly the same

Immutability

Variables are immutable, however values can be shadowed, so I can create a shadowing x based on a previous x like so:

Terminal window
1
let x = $x + 1

Scoping

Nushell uses scoped environmments in blocks, so a command can use a value within its scope like so:

Terminal window
1
ls | each { |it|
2
cd $it.name
3
make
4
}

Fundamentals

Types of Data

The describe command returns the type of a variable

Terminal window
1
42 | describe

Conversions

The into command can convert variable types

Terminal window
1
"-5" | into int
Terminal window
1
"1.2" | into decimal

Strings

Strings can be created as:

  1. Double quotes: "hello world"
  2. Single quotes: 'hello: "world"'
  3. Interpolated: $"my number = (40 + 2)"
  4. Bare: hello

Bools

Booleans are simply true and false

Dates

Dates can be in the following formats:

  • 2022-02-02
  • 2022-02-02T14:30:00
  • 2022-02-02T14:30:00+05:00

Durations

Nushell has the following durations:

  • ns nanosecond
  • us microsecond
  • ms millisecond
  • sec second
  • min minute
  • hr hour
  • day day
  • wk week

And can be used like so:

Terminal window
1
3.14day

Or in calculations

Terminal window
1
30day / 1sec

Ranges

Ranges can be done as 1..3 for example, by default the end is inclusive, ranges can also be open ended ..2 or 2..

Records

Records hold key-value pairs, and may or may not have commas between entry names:

Terminal window
1
{name: john age: 5}

A record is the same as a single row of a table

Records can be iterated over by transposing them into a table:

Terminal window
1
{name: john age: 5} | transpose key value

And accessing properties can be done like:

Terminal window
1
{name: john age: 5}.age

Or

Terminal window
1
{name: john age: 5}."age"

Lists

Lists are ordered sequences of data and use [] with optional , separators. The below will create alist of strings

Terminal window
1
[sam fred george]

A list is the same as a single column table

Indexing lists can be done with a . as with records:

Terminal window
1
[sam fred george].1

Or using ranges:

Terminal window
1
[sam fred george] | range 0..1

Tables

Tables can be created with the following syntax:

Terminal window
1
[[column1, column2]; [Value1, Value2] [Value3, Value4]]

Tables can also be created from json

Terminal window
1
[{name: sam, rank: 10}, {name: bob, rank: 7}]

Internally tables are just a list of records

Blocks

Blocks of code are denoted using {}, for example:

Terminal window
1
each { |it| print $it }

Loading Data

Open Files

Files can be opened with the open command:

Terminal window
1
open package.json

Nu will parse the file if it can and will return data and not just a plain string

If a file extension isn’t what the type usually has, we can still parse the file, we just ned to tell nu that it’s a specific format, so we can do this like so:

Terminal window
1
open Cargo.lock | from toml

Manipulating Strings

String data can be manipulated using things like the lines command which will split each line into a row:

Terminal window
1
open people.txt | lines

And we can further apply the split command on the column to split it by some specific character:

Terminal window
1
open people.txt | lines | split column ";"

Additionally, we can use trim:

Terminal window
1
open people.txt | lines | split column ";" | str trim

And lastly, we can transform it into a table with formal column names with some additional properties on the split command:

Terminal window
1
open people.txt | lines | split column "|" first_name last_name job | str trim

Fetch Urls

We can also fetch remote files which will then also be converted into data like so:

Terminal window
1
fetch https://blog.rust-lang.org/feed.xml

Cheatsheet

Moving around the File System

Nushell provides commands for normal file-system related tasks which are similar to common commands such as:

Terminal window
1
./hello/world # will cd to the directory

Listing Files

Terminal window
1
ls

Or listing a specific file type

Terminal window
1
ls *.md

Or even globs

Terminal window
1
ls **/*.md

Globs

You can also use the glob method directly to find files recursively:

Terminal window
1
glob **/*.png

The glob method returns a list of strings versus the ls method which returns a list of file objects

Deleting Branches

We can use a script like the following to delete all git branches other than the current branch

Terminal window
1
git branch | lines | filter {|l| $l | str contains -n "*"} | each {|b| $b | str trim} | each {|b| git branch -d $b}

Stopping All Docker Containers

The Docker CLI outputs data that’s nicely structured for working with the NuShell table structure.

We can kill all containers by parsing the data into a table and stopping them individually

Terminal window
1
docker container ls | from ssv | select "CONTAINER ID" | each { |i| docker container stop $i."CONTAINER ID" }