A Nu-er Shell
03 October 2024
Updated: 04 October 2024
Take a look at Nushell
ZOOOOOOM!
Is everything big enough and are we in dark mode?
Nushell
Go to demo
Nushell is a shell that makes use of the Nu Programming Language
- Cross-platform
- Everything is data
- Extensible
- Composition based
- Strongly typed
Data
- Traditional shells, e.g. bash, treat everything as strings
- Nushell treats input streams as data that can be transformed
- Uses the Nu Language for transforming data
- Wraps common commands into data-rich ones, e.g.
ls
,open
1> ls2
3╭───┬─────────────┬──────┬───────────┬────────────────╮4│ # │ name │ type │ size │ modified │5├───┼─────────────┼──────┼───────────┼────────────────┤6│ 0 │ config.nu │ file │ 1.000 B │ a month ago │7│ 1 │ env.nu │ file │ 4.3 KiB │ a month ago │8│ 2 │ history.txt │ file │ 193.6 KiB │ 12 minutes ago │9│ 3 │ scripts │ dir │ 256 B │ 2 months ago │10╰───┴─────────────┴──────┴───────────┴────────────────╯
Pipelines
- A functional programming technique
- Feels a bit like LINQ
- Can be chained together and composed
1git log --oneline | lines | parse $template
- Lots of builtin function, e.g.
lines
,parse
,map
,where
,find
,filter
,each
,glob
,watch
… and more!
File Formats
- Supports many common file formats as data
- CSV
- JSON
- YAML
- Can add support for custom formats easily
- Easily convert between simple formats
1open data.csv | to json | save data.json
The Nu Language
1# compose with command utilities2let contents = cat my-file.txt3
4print $contents5
6# define custom functions/commands7def "my custom command" [name:string] {8 "Hello " + $name9}10
11# use a module12use my_module.nu13
14# load a source file into scope15source my_file.nu16
17# create an alias18alias gito = git log --oneline
Demo
Examples below
Nushell
- Supports pretty much anything available in a file
- Easily configurable -
$nu.config-path
Show my setup
Examples
1const url = 'https://dummyjson.com/todos'2
3# This is the main method, running --help will show docs for all subcommands4def example [] {5 print "Working with HTTP Data"6}7
8def "example get_full" [] {9 http get $url --full10}11
12def "example filter_todos" [] {13 http get $url14 | get todos15 | where completed == false16 | select id todo userId17 | to yaml18}19
20
21let template = '{ip} - - [{date}] "{method} {url} {protocol}" {status} {size} "{served}" "{useragent}"'22
23
24let logs = "https://raw.githubusercontent.com/elastic/examples/refs/heads/master/Common%20Data%20Formats/apache_logs/apache_logs"25
26def "example download_logs" [] {27 http get $logs28 | save http-logs.txt29}30
31def "example parse_logs" [] {32 open http-logs.txt33 | lines34 | parse $template35}36
37def "example filter_logs" [] {38 example parse_logs39 | where method == POST40 | select url status useragent41}42
43def "example filter_pngs" [] {44 example parse_logs45 | where method == GET46 | where url =~ ".png"47 | where size != "-"48 | into int size49 | where size > 500_00050 | select url status served size51}52
53def "example live_convert" [] {54 watch input.csv { open input.csv | to json | save output.yaml }55}56
57alias helpme = example --help