| name | nushell |
| description | Process structured data in pipelines, filter tables, transform JSON/CSV/YAML, and define custom commands. Use when scripting with typed parameters or working with tabular data. |
nu-shell
Structured-data shell. Pipelines work on tables, lists, and records, not only text.
Core Concepts
Data Types
- Record:
{ name: "John", age: 30 }
- List:
[1, 2, 3]
- Table: list of records with same fields
Pipelines
ls | where size > 10mb | sort-by size
Running Scripts
nu myscript.nu
nu -c 'ls | length'
source myscript.nu
Data Manipulation
Loading and Saving
let config = (open config.json)
let data = (open data.csv)
$data | save output.yaml
$data | to json | save output.json
Filtering and Selecting
ls | where name =~ "test" # Filter rows
ls | select name size # Select columns
(open package.json).version # Access fields
Processing Tables
ls | where size > 10mb # Filter by condition
ls | select name size # Select columns
ls | sort-by size # Sort
ls | group-by name # Group
ls | length # Count rows
Processing Records
let user = { name: "John", age: 30 }
echo $user.name # Access field
let updated = { ...$user, age: 31 } # Update field
let merged = { ...$config, debug: true } # Merge
Processing Lists
[1, 2, 3, 4, 5] | where $it > 2 # Filter
[1, 2, 3] | each { |x| $x * 2 } # Map
[1, 2, 3, 4, 5] | reduce { |acc, x| $acc + $x } # Reduce
Scripting
Basic Script Structure
#!/usr/bin/env nu
def "my command" [param: string] {
echo $"Hello, ($param)"
}
my command "world"
Control Flow
# If statement
if true { echo "Hello" }
# If-else
if true { echo "Yes" } else { echo "No" }
# For loop
for i in 1..10 { echo $i }
# While loop
mut i = 1
while $i <= 10 {
echo $i
$i = $i + 1
}
Custom Commands
# With typed parameters
def "create project" [name: string, type: string = "typescript"] {
echo $"Creating ($name) with ($type)"
}
# With flags
def "deploy" [--env: string = "production"] {
echo $"Deploying to ($env)"
}
File Operations
let content = (open "file.txt") # Read
$content | save "output.txt" # Write
$content | save --append "file.txt" # Append
Tips
- Use
nu -c 'command' for inline runs
- Use
open to load many data formats
- Use
save to write many data formats
- Use
where to filter tables
- Use
select to pick columns
- Use
sort-by to sort tables
- Use
each to map lists
- Use
reduce to combine list items
- Use
def for custom commands with typed params
- Use
... to merge records