3 -description: Nushell shell and scripting — structured data pipelines, $env variables, bash-to-nushell conversion, redirection gotchas. Use when working with nushell scripts, shell commands, or converting bash patterns.
3 +description: Nushell shell and scripting — structured pipelines, current syntax, config, modules, redirection, errors, and Bash-to-Nu translation. Use when editing .nu files or converting shell snippets.
9 -Modern shell with structured data pipelines.
10 -Data flows as tables/lists/records, not text.
9 +Target: Nushell 0.114+.
12 -##
Help First (Built-in) 11 +##
Trust local help first 15 -help # General help overview
16 -help commands # List all 460+ commands
17 -help <command> # Help for specific command
18 -help --find "keyword" # Search commands by keyword
19 -help operators # All operators and precedence
20 -help <keyword> # keywords: if, match, for, try, etc.
14 +version | get version
15 +help commands | where name =~ "str "
16 +help commands | where name == "path type" | get input_output
17 +help --find semver | get name
24 -`help` output is structured data.
25 -Use `| where` to filter.
20 +`help commands` is structured. `help <command>` is human text.
24 +- Pipelines pass values: strings, records, lists, tables, not text lines.
25 +- Env vars live in `$env`: `$env.PATH`, `$env.LAST_EXIT_CODE`, `$env.config`.
26 +- Missing optional env/record fields: `$env.PWD?`, `$row.name?`.
27 +- Optional params without defaults are `oneof<T, nothing>`; guard or default them.
28 +- `null` is `nothing`; commands like `path type` reject it. Guard first.
34 +| `$VAR` | `$env.VAR` |
35 +| `export A=b` | `$env.A = "b"` |
36 +| `$?` | `$env.LAST_EXIT_CODE` |
37 +| `$(cmd)` | `(cmd)` |
38 +| `cat file` | `open --raw file` |
39 +| `grep x` | `lines | where $it =~ "x"` for text; `where col =~ "x"` for tables |
40 +| `find . -name '*.nu'` | `ls **/*.nu` |
41 +| `[ -f file ]` | `("file" | path type) == file` |
42 +| `basename "$p"` | `$p | path basename` |
43 +| `2>&1` | `o+e>|` on external commands |
45 +No shell `&&`/`||`. Use `;` for sequencing, `and`/`or` for booleans, or inspect `$env.LAST_EXIT_CODE`.
28 -# Find all filter commands
29 -help commands | where category == "filters"
31 -# Find commands with "json" in description
32 -help --find "json" | get name
50 +"hello" | save out.txt
51 +"hello" | save --append out.txt
54 +try { internal-command } catch {|err| $err.msg }
57 +`e>|`/`o+e>|` are for external commands, not Nu internals.
37 -| Variable | Purpose |
38 -| ------------- | ---------------------------------------------------------------- |
39 -| `$nu` | Runtime info: config-path, env-path, current-exe, is-interactive |
40 -| `$env` | Environment variables |
41 -| `$env.config` | Configuration record |
42 -| `$in` | Current pipeline input |
43 -| `$it` | Current row element (in `where`, `each`) |
46 -Env vars start with `$env.` because "environment is where env vars live".
48 -##
Bash → Nushell Cheat Sheet 51 -| --------------------- | ------------------------- |
52 -| `$VAR` | `$env.VAR` |
53 -| `export VAR=val` | `$env.VAR = val` |
54 -| `$?` | `$env.LAST_EXIT_CODE` |
55 -| `$(cmd)` | `(cmd)` |
56 -| `&&` / `\|\|` | `;` |
57 -| `cat file` | `open --raw file` |
58 -| `grep pattern` | `where $it =~ pattern` |
59 -| `find . -name "*.rs"` | `ls **/*.rs` |
60 -| `[ -f file ]` | `$file |
61 -| `basename path` | `$path |
62 -| `2>&1` | `o+e>\|` (external only!) |
59 +##
Strings and external commands 68 -ls | where size > 1mb
69 -ls | where type == dir
70 -ls | where name =~ "^nu"
73 -ls | select name size
74 -ls | insert big {|row| $row.size > 1mb }
75 -ls | update name { path parse | get stem }
78 -[1 2 3] | each {|x| $x * 2 }
79 -[1 2 3] | where $it > 2
80 -[1 2 3] | reduce {|val acc| $acc + $val }
65 +^git --no-pager log --oneline
83 -##
Redirection (Critical!) 69 +Use `str lowercase` / `str uppercase` for case changes.
87 -"hello" | save output.txt
88 -"hello" | save --append output.txt
90 -# Redirect stderr (EXTERNAL COMMANDS ONLY!)
91 -^external_cmd e>| ignore # stderr to /dev/null
92 -^external_cmd o+e>| ignore # both streams to /dev/null
93 -^external_cmd o+e>| error.log # both to file
95 -# For internal commands - NO stderr redirection
96 -command | ignore # discard output
97 -try { risky } catch { ... } # catch errors
101 -`e>|` only works with `^external_cmd`, not internal commands.
106 -# Immutable (most cases)
109 -let cfg = {theme: dark}
111 -# Constants (compile-time)
114 -# Mutable (rarely needed)
118 -# Env vars (mutable globally)
119 -$env.PATH = ($env.PATH | prepend "/custom")
125 -'hello world' # literal
126 -"hello\nworld" # escapes
127 -$"hello, ($name)!" # interpolation
128 -$'raw string'# # no escapes
129 -r#'can have "quotes"# # raw with quotes
140 -{a: 1, b: 2} # record
141 -[{x: 1}, {x: 2}] # table
142 -10mb, 1gb # filesize
143 -5sec, 2min # duration
144 -1..10 # range (inclusive)
145 -1..<10 # range (exclusive)
151 -def greet [name: string] { $"Hello, ($name)!" }
152 -def add [a: int, b: int] { $a + $b }
153 -def sum [...nums: int] { $nums | math sum }
159 -if $cond { "yes" } else { "no" }
167 -for i in 1..10 { print $i }
179 -error make "something failed"
180 -error make {msg: "failed", help: "try this"}
184 -`$err` has `msg`, `debug`, `json` fields.
185 -No direct `code` field.
187 -##
External Commands 190 -^ls -la # explicit external
194 -let args = ["-la", "*.nu"]
202 - export def add [a: int, b: int] { $a + $b }
213 -ls **/*.rs # recursive glob
214 -open file.json # parses structured data
215 -open --raw file.txt # raw text
216 -"/tmp" | path exists # check existence
222 -nu script.nu # run file
223 -nu -c 'ls | where ...' # one-liner
228 -###
1. Command not found? 231 -# Check if command exists
232 -help commands | where name == "cmd"
233 -which cmd # finds command location
242 -# Check what type a value is
74 +ls | where type == file | select name size
75 +open data.json | get items | each {|x| $x.name }
76 +[1 2 3] | reduce {|x acc| $acc + $x }
245 -# Check command input/output types
246 -help <command> | get input_output
249 -###
3. Structured data issues? 255 -# Explore nested data
84 +let p = "~/x" | path expand
88 +$p | path join "child"
259 -###
4. Pipeline not working? 91 +Guard nullable paths:
263 -command | debug | your_command
265 -# Check what's flowing
94 +let q = [$p] | compact | first
95 +let t = if $q == null { null } else { try { $q | path type } catch { null } }
269 -###
5. Environment issues? 98 +##
Functions, modules, scripts 273 -$env | transpose | sort-by column0
101 +def greet [name: string = "world"] { $"hi ($name)" }
102 +def flaggy [--upper, name: string] { if $upper { $name | str uppercase } else { $name } }
103 +flaggy -- -value-that-starts-with-dash
275 -# Check specific var
105 +module util { export def double [x: int] { $x * 2 } }
109 +Submodules are not implicitly imported in 0.114+. Re-export them explicitly with `export use sub`.
281 -**Use built-in `help` first** - it's faster and always accurate for your
114 +"hello" | run transform.nu | str uppercase
284 -| Resource | When to Use |
285 -| ---------------------------------------------------------------------- | ------------------------------------ |
286 -| `help commands` | Find commands, check signatures |
287 -| `help <cmd>` | Command details, examples |
288 -| [nushell.sh/book](https://www.nushell.sh/book) | Conceptual overview, getting started |
289 -| [nushell.sh/commands](https://www.nushell.sh/commands/) | Full command reference |
290 -| [nushell.sh/cookbook](https://www.nushell.sh/cookbook/) | Ready-made recipes |
291 -| [nushell.sh/lang-guide](https://www.nushell.sh/lang-guide/) | Language specification |
292 -| [github.com/nushell/nu_scripts](https://github.com/nushell/nu_scripts) | Community scripts, cool one-liners |
295 -[nushell.sh/book/quick_tour.html](https://www.nushell.sh/book/quick_tour.html) -
296 -covers basics in 5 minutes.
120 +try { error make {msg: "failed", help: "check input"} } catch {|err|
298 -##
Common Mistakes (from real nushell use) 126 +Error records include `msg`, `debug`, `raw`, `rendered`, `details`.
300 -1. **`2>&1`** → `o+e>\|` (for external commands)
128 +##
Current 0.114 notes 302 -2. **`$err.code`** → `$err.msg` (code is in `$err.json`)
304 -3. **`ls | e>| ignore`** → Internal commands don't support stderr redirection.
307 -4. **`$variable`** when meaning env var → `$env.variable`
309 -5. **`$(cmd)`** → `(cmd)`
311 -6. **Forgetting `^`** before external command → Use `^cmd` to be explicit
313 -7. **Pipe structured data expecting strings** → `help` returns tables, not
315 - Use `| get column` not `| grep`.
317 -8. **`$config`** → `$env.config` (old syntax deprecated)
319 -9. **Assigning to `$var`** → Nushell vars are immutable.
320 - Use `mut` or `$env.var`.
322 -10. **`test -f file`** → `$file | path exists`
130 +- `run` lets scripts act as pipeline stages.
131 +- `into semver` and `semver *` commands handle semantic versions.
132 +- `--` stops option parsing for Nu commands.
133 +- Runtime assignment annotations are checked by default.
134 +- `from xlsx` / `from ods` use `--noheaders` and `--first-row`.
135 +- `grid` takes list/table input; pass a column name when needed.