Nushell
Updated: 23 December 2025
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:
1let x = $x + 1Scoping
Nushell uses scoped environmments in blocks, so a command can use a value within its scope like so:
1ls | each { |it|2 cd $it.name3 make4}Fundamentals
Types of Data
The describe command returns the type of a variable
142 | describeConversions
The into command can convert variable types
1"-5" | into int1"1.2" | into decimalStrings
Strings can be created as:
- Double quotes:
"hello world" - Single quotes:
'hello: "world"' - Interpolated:
$"my number = (40 + 2)" - Bare:
hello
Bools
Booleans are simply true and false
Dates
Dates can be in the following formats:
2022-02-022022-02-02T14:30:002022-02-02T14:30:00+05:00
Durations
Nushell has the following durations:
nsnanosecondusmicrosecondmsmillisecondsecsecondminminutehrhourdaydaywkweek
And can be used like so:
13.14dayOr in calculations
130day / 1secRanges
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:
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:
1{name: john age: 5} | transpose key valueAnd accessing properties can be done like:
1{name: john age: 5}.ageOr
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
1[sam fred george]A list is the same as a single column table
Indexing lists can be done with a . as with records:
1[sam fred george].1Or using ranges:
1[sam fred george] | range 0..1Tables
Tables can be created with the following syntax:
1[[column1, column2]; [Value1, Value2] [Value3, Value4]]Tables can also be created from json
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:
1each { |it| print $it }Filtering
The most common way to filter data is using where, this has a few different ways it can be used
The structure of where can be some kind of comparison of the form where <field> <comparator> <value>, e.g. ~= does a “contains” comparison
1ls | where name =~ "package"Alternatively, we can use $it to reference to a given input value, like where <expression>:
1ls | where ($it.name | str starts-with "package")$it is used for referencing each row of the input list. If we want to do something more complex, you can also use a closure:
1ls | where {|i| $i.name | str starts-with "package"}Or, inferring the the closure param with $in to make it easier to type:
1ls | where {$in.name | str starts-with "package"}Loading Data
Open Files
Files can be opened with the open command:
1open package.jsonNu 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:
1open Cargo.lock | from tomlManipulating Strings
String data can be manipulated using things like the lines command which will split each line into a row:
1open people.txt | linesAnd we can further apply the split command on the column to split it by some specific character:
1open people.txt | lines | split column ";"Additionally, we can use trim:
1open people.txt | lines | split column ";" | str trimAnd lastly, we can transform it into a table with formal column names with some additional properties on the split command:
1open people.txt | lines | split column "|" first_name last_name job | str trimFetch Urls
We can also fetch remote files which will then also be converted into data like so:
1fetch https://blog.rust-lang.org/feed.xmlCheatsheet
Moving around the File System
Nushell provides commands for normal file-system related tasks which are similar to common commands such as:
1./hello/world # will cd to the directoryListing Files
1lsOr listing a specific file type
1ls *.mdOr even globs
1ls **/*.mdGlobs
You can also use the glob method directly to find files recursively:
1glob **/*.pngThe
globmethod returns a list of strings versus thelsmethod which returns a list of file objects
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
1docker container ls | from ssv | select "CONTAINER ID" | each { |i| docker container stop $i."CONTAINER ID" }Config
Some utils from my current config.nu, primarily for working with Git
1alias gch = git checkout2alias gcb = git checkout -b3alias glg = git log --graph4alias ga = git add5alias gp = git push6alias gf = git fetch7alias gl = git pull8alias gcm = git commit -m9alias gprune = git remote prune origin10
11alias conf = code $nu.config-path12alias env = code $nu.env-path13
14## Deletes all branches other than the current branch15def gclean [] {16 git branch17 | lines18 | filter {|l| $l | str contains -n "*"}19 | each {|b| $b | str trim}20 | each {|b| git branch -d $b}21}22
23def 'gclean D' [] {24 git branch25 | lines26 | filter {|l| $l | str contains -n "*"}27 | each {|b| $b | str trim}28 | each {|b| git branch -D $b}29}30
31def gmaster [] {32 let branch = git rev-parse --abbrev-ref HEAD33 git checkout master34 git pull35 git checkout $branch36 git merge master37}38
39def dev [repo:string] {40 code $"~/repos/$repo"41}42
43## Search for a string or regex using `rg -i`44def search [45 regex:string, # regex or string to search on46 -i # Run the search as case insensitive47 ] {48 if $i {49 rg -i $regex50 } else {51 rg $regex52 }53}Watch Mode
Nushell has builtin support for watching files and running a comand when they change
You can do this using the watch command:
1watch /some/path { echo "things have changed" }2watch /some/path {|op, path, new_path| echo "things have changed" }3watch /some/path --glob=**/*.json { echo "things have changed" }Notifiy
A little script that’s also useful to have is this one that will notify you when a task completes. It’s a handy way to be notified when a long running task completes
This uses AppleScript as per the example on stackexchange so it will only work on MacOS. I’m sure there’s a similar way to accomplish this on other operating systems
1def "notify" [title: string = "Task Complete"] {2 print $in3
4 let $command = 'display notification "' + $title + '" with title "Shell Process Complete" sound name "Frog"'5 osascript -e $command6}You can include the above in your nushell config and use it as follows:
1my-command long-task | notify "My Long Task is Completed"It will also handle printing the output from the task being run
The $in value and closures
Pass streams as arguments using $in
The $in can be implicitly accessed as the value that’s piped into another command. Basically, this means that the following tao commands are equal:
1## passing it normally2echo (open hello.txt)3
4## passing with $in5open hello.txt | echo $inPassing Multiple Strings
Nushell supports a spread-type operator for passing a list from input into a space-separated command kind of like xargs:
1ls *.json | get name | yarn prettier --write ...$inThe
...$inspreads the input stream into a space-separated list
Usage with Functions and Closures
Nushell functions can also use an implicit input parameter, this can be used when defining a function, for example:
1def example[] {2 echo $in3}Which can then be used as
1"Hello World!" | exampleAdditionally, note that example "Hello World!" will not work since $in params cannot be passed positionally and can only be used part of a pipeline
It’s also possible to use $in when we epect a closure which lets us leave out the parameter definition, for example, we can run ls in all subdirectories of an input like so:
1## Using a normal closure2ls | each { |f| ls $f.name }3
4## Using `$in`5ls | each { ls $in.name }The { ls $in.name } is the same as a closure like {|f| ls $f.name } so it’s a bit easier to type in this scenario as well.
Parsing
The parse function can be used to read some string into a usable data structure, take the following file for example:
1john smith, age: 242jack smith, age: 54The parse command lets us structure that using:
1open data.txt | lines | parse "{name} {surname}, age: {age}"2
3╭───┬──────┬─────────┬─────╮4│ # │ name │ surname │ age │5├───┼──────┼─────────┼─────┤6│ 0 │ john │ smith │ 24 │7│ 1 │ jack │ smith │ 54 │8╰───┴──────┴─────────┴─────╯Detecting Columns
In simple cases instead of parsing some text you can also use detect columns. For example using a file like this:
1Name Age2Bob Smith 253Jack Smith 82We can use detect columns to automatically parse the simple structure for us:
1open data.txt | detect columns2
3╭───┬──────┬─────╮4│ # │ Name │ Age │5├───┼──────┼─────┤6│ 0 │ Bob │ 25 │7│ 1 │ Jack │ 82 │8╰───┴──────┴─────╯If our table doesn’t have headers we can still use detect columns --no-headers to prevent it trying to use the first row as a header:
1 git status --porcelain | detect columns --no-headers2
3╭───┬─────────┬──────────╮4│ # │ column0 │ column1 │5├───┼─────────┼──────────┤6│ 0 │ A │ data.txt │7╰───┴─────────┴──────────╯We can combine this with a rename to improve this structure of our output table:
1git status --porcelain | detect columns --no-headers | rename status file2
3╭───┬────────┬──────────╮4│ # │ status │ file │5├───┼────────┼──────────┤6│ 0 │ A │ data.txt │7╰───┴────────┴──────────╯Input
You can take in user input using the input function, this allows for dynamic imput. This is handy for doing a search over some list, for example composing it with the above:
1open data.txt | lines | parse "{name} {surname}, age: {age}" | input list 'Search for User' --fuzzyClosures
Nushell does something quite interesting with closures. Since everything is immutable it’s possible to do environment-changing operations in a somewhat contained way.
For example, I can do some stuff like moving to a different folder, but I will not be affected outside of the closure
1## in the `root` folder2do { cd ./my-child | ls } # within the closure i am inside of the `my-child` folder3## back to the `root` folderOr I can cd into each folder and ls each of them, while remaining in my parent folder.
1## in the `root` folder2ls | where type == dir | each { cd $in.name | ls }3## back to the `root` folderParallel
Due to the isolation that closures afford us, we can also run these in parallel, nushell has parallel methods of some commands, e.g. the each command, which can be used with par-each:
1ls | where type == dir | par-each { cd $in.name | ls }This works the same but is much faster for large/complex tasks
Timer
Nushell also has a timeit command that can be used to time the execution of any block, for example:
1timeit { ls | each { print $in.name } }Pipes
Pipes are done using |, for example:
1cat myfile.txt | linesWe can also more specifically pipe the stdout or strerr streams using the following syntaxes
1## stdout only2dostuff | lines3
4## stderr only5dostuff err>| lines6dostuff e>| lines7
8## stderr + stdout9dostuff out+err> lines10dostuff o+e> linesCompletion
You can also capture the stdout, stderr, and exit_code using the complete command like so:
1cat myfile.txt | complete2
3╭───────────┬────────────────────────────────────────────╮4│ stdout │ │5│ stderr │ cat: myfile.txt: No such file or directory │6│ │ │7│ exit_code │ 1 │8╰───────────┴────────────────────────────────────────────╯