Nushell

General

(
    "01/22/2021" |
    parse "{month}/{day}/{year}" |
    get year
)

PATH configuration

$env.PATH = ($env.PATH | split row (char esep) | append '/opt/homebrew/bin')

Data types

let greet = { |name| print $"Hello ($name)"}
do $greet "Julian"
mut x = 1
if true {
    $x += 1000
}
print $x

Basic commands

Working with strings

Working with tables

Tip

Remember that column indices are strings, and row indices are numbers.

Examples

open package.json | get scripts

Working with lists

# Do any color names end with "e"?
$colors | any {|it| $it | str ends-with "e" } # true

Iterating over lists

let names = [Mark Tami Amanda Jeremy]
$names | each { |it| $"Hello, ($it)!" }
# Outputs "Hello, Mark!" and three more similar lines.

$names | enumerate | each { |it| $"($it.index + 1) - ($it.item)" }
# Outputs "1 - Mark", "2 - Tami", etc.
let colors = [red orange yellow green blue purple]
$colors | where ($it | str ends-with 'e')
# The block passed to `where` must evaluate to a boolean.
# This outputs the list [orange blue purple].
let scores = [3 8 4]
$"total = ($scores | reduce { |it, acc| $acc + $it })" # total = 15

$"total = ($scores | math sum)" # easier approach, same result

$"product = ($scores | reduce --fold 1 { |it, acc| $acc * $it })" # total = 96

$scores | enumerate | reduce --fold 0 { |it, acc| $acc + $it.index * $it.item } # 0*3 + 1*8 + 2*4 = 16

Examples

Kill single process by name

kill (ps | where name == "Dock" | first).pid

Kill matching processes

ps | where name == "Dock" | each { kill $in.pid }