cheat-sheets

Nushell

Nushell (nu) is a cross-platform shell where commands pass structured data (tables, records, lists) instead of text, so ls output can be filtered like a database. Use it for interactive data wrangling or readable scripts; it is not POSIX-compatible, so keep portable scripts in Bash. Pick between shells with Shell Comparison.

Documentation: The Nushell Book, Command reference Project: Nushell

Checked against the official documentation for Nushell 0.116.1. Nushell was not installed or run when writing this sheet, and the project is pre-1.0, so commands change between releases: check help COMMAND on your version. Rows marked (unverified) were not found in the pages that were checked.

Variables and Environment #

COMMANDDESCRIPTION
let x = 1Immutable variable
mut x = 1; $x += 1Mutable variable (mut, not let mut)
const X = 1Constant, evaluated at parse time
$env.NAME = "value"Set an environment variable (current scope)
$env.NAMERead an environment variable
load-env {A: "1", B: "2"}Set several at once from a record
$env.PATH = ($env.PATH | prepend '/opt/tool/bin')Prepend to PATH (a list, not a colon-separated string)
use std/util "path add"; path add '/opt/tool/bin'Add to PATH with the standard library helper
def --env cdd [] { cd /tmp }Command whose environment changes (such as cd) persist after it returns

Strings #

COMMANDDESCRIPTION
$"hello ($name)"Interpolation; escapes like \n work
$'hello ($name)'Interpolation, no escape processing
"a,b" | split row ","Split into a list
["a" "b"] | str join "-"Join a list
$s | str replace "old" "new"Replace (first match; --all for all)
$s | str contains "x"Substring test
"a b" | parse "{first} {second}"Extract fields into a table

Pipelines and Data #

Everything below works on tables and lists. Row marked (unverified) means the command or flag was not read in the docs checked, although it is listed in the command reference.

COMMANDDESCRIPTION
ls | where size > 1mb | sort-by modifiedFiles over 1 MB, oldest first (unverified)
ls | get nameOne column as a list (unverified)
ls | select name sizeKeep chosen columns (unverified)
$t | where col == "x"Filter rows (unverified)
$t | sort-by colSort (unverified)
$t | group-by colGroup (unverified)
$list | each {|x| $x * 2}Map with a closure (unverified)
$list | par-each {|x| $x * 2}Map in parallel (--keep-order keeps input order)
$list | reduce {|it, acc| $acc + $it}Fold (unverified)
$t | transposeSwap rows and columns
1..5Range, end inclusive
$inThe pipeline input inside a block or command
{|x| $x * 2}Closure syntax
ps, sys host, du, which NAMEProcess table, host information, disk usage, locate a command

Files and Formats #

COMMANDDESCRIPTION
open file.jsonRead and parse by extension into structured data
open --raw file.txtRead as plain text
$data | save out.jsonWrite (format chosen by extension)
$data | save --append out.txtAppend (-a)
$data | save --force out.jsonOverwrite an existing file (-f)
$data | to yamlConvert to YAML
$data | to nuonConvert to Nushell’s own data notation
http get https://example.com/apiHTTP GET, parsed if the response is JSON
from json, to json, from csv, to csvConvert between text and data ((unverified): not read in the docs checked)

External Commands and Errors #

COMMANDDESCRIPTION
^ls -lRun the external command, bypassing a Nushell command of the same name
^cmd | completeCapture stdout, stderr and exit_code as a record
$env.LAST_EXIT_CODEExit code of the last external command
do -i { ^cmd }Run a block and ignore errors (--ignore-errors)
try { ... } catch {|err| ... }Handle errors
timeit { ^cmd }Time a block
print "text"Print to stdout
input "Name: "Prompt for a line

Control Flow #

nu
if $x > 1 { "big" } else { "small" }

match $x {
    1 => "one"
    _ => "other"
}

for i in 1..3 { print $i }

mut n = 0
while $n < 3 { $n += 1 }

loop { break }

Custom Commands and Scripts #

nu
#!/usr/bin/env nu

# Greet someone
def greet [
    name: string          # required, typed
    count?: int           # optional
    --shout (-s)          # flag
    ...rest: string       # remaining arguments
] {
    let msg = $"hello ($name)"
    if $shout { $msg | str upcase } else { $msg }
}

def main [name: string] {
    greet $name
}

Run a script with nu script.nu or a one-liner with nu -c 'ls'. In a script, def main is the entry point and its parameters become the command-line arguments.

COMMANDDESCRIPTION
source file.nuRun a file in the current scope (parse-time path)
use module.nuLoad a module’s commands
overlay use module.nuActivate a module as a removable overlay (overlay hide removes it)
alias ll = ls -lAlias (note the =)

Configuration #

COMMANDDESCRIPTION
config nuOpen config.nu for editing
config envEdit env.nu
$nu.config-pathPath to the active config.nu (unverified)
$env.config.show_banner = falseHide the start-up banner
$env.config.edit_mode = 'vi'Vi key bindings (emacs is the other common option)
help commandsList every command
help COMMANDHelp for one command
history, history --longShow history (--long adds timestamps and more detail)
Ctrl+ROpen the history menu

For external completions, Nushell’s docs show carapace wired up as $env.config.completions.external.completer (see the custom completions page).

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.