Skip to content

Commit 39883d8

Browse files
authored
Update docs for 0.92.0 (file, pipes, and io) (#1352)
* Update docs for 0.92.0 (file, pipes, and io) * Fix typo * Update links
1 parent fa0abf7 commit 39883d8

16 files changed

+158
-213
lines changed

.vuepress/configs/sidebar/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const sidebarEn: SidebarConfig = {
4242
'/book/custom_commands.md',
4343
'/book/aliases.md',
4444
'/book/operators.md',
45-
'/book/variables_and_subexpressions.md',
45+
'/book/variables.md',
4646
'/book/control_flow.md',
4747
'/book/scripts.md',
4848
'/book/modules.md',

blog/2021-02-16-nushell_0_27.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The Nushell book received some much-needed updates. You'll notice new chapters a
3232
- [Updated section on data types](https://www.nushell.sh/book/types_of_data.html)
3333
- [A new section on creating your own commands](https://www.nushell.sh/book/custom_commands.html)
3434
- [An updated section on aliases](https://www.nushell.sh/book/aliases.html)
35-
- [A new section on working with variables and expressions](https://www.nushell.sh/book/variables_and_subexpressions.html)
35+
- [A new section on working with variables and expressions](https://www.nushell.sh/book/variables.html)
3636
- [The start of a new section on writing your own Nushell scripts](https://www.nushell.sh/book/scripts.html)
3737

3838
# Improvements

blog/2023-06-27-road-to-1_0.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Today we release the 0.82 version of Nushell. Not only will it be our 65th publi
1313

1414
Nushell reached a much wider user base in the last few months. On GitHub, Nushell is now officially recognized for [syntax highlighting](https://github.com/github-linguist/linguist/releases/tag/v7.26.0) and recently passed 25,000 stars. We are also actively thinking about how we want to move towards stabilization for our 1.0 release.
1515

16-
Thus, we want to use this opportunity to share our general thoughts and announce that we will be slightly slowing down our regular release schedule.
16+
Thus, we want to use this opportunity to share our general thoughts and announce that we will be slightly slowing down our regular release schedule.
1717
Our focus going forward will be getting Nushell to a high-quality 1.0, one that has a supported language that is reliable and backward-compatible, and a high-quality shell experience to match.
1818

1919
## A four-week release schedule
@@ -35,11 +35,11 @@ Apart from rewriting the engine, it included our own line editor [reedline](http
3535
The rewrite unlocked many improvements and opportunities for valuable experimentation.
3636

3737
Since `0.60`, we achieved a much more mature system of [modules](https://www.nushell.sh/book/modules.html) and [overlays](https://www.nushell.sh/book/overlays.html) that allow you to build more composable applications in Nu.
38-
As part of this effort to strengthen the programming language chops of Nu, we also spent a lot of time finding the balance between our [functional programming philosophy of pipelined data](https://www.nushell.sh/book/thinking_in_nu.html) and the [mutation of variables](https://www.nushell.sh/book/variables_and_subexpressions.html#mutable-variables) to allow simple imperative patterns.
38+
As part of this effort to strengthen the programming language chops of Nu, we also spent a lot of time finding the balance between our [functional programming philosophy of pipelined data](https://www.nushell.sh/book/thinking_in_nu.html) and the [mutation of variables](https://www.nushell.sh/book/variables.html#mutable-variables) to allow simple imperative patterns.
3939

4040
We also saw a few efforts that didn't pan out yet. For example, we tried to integrate the [Polars](https://github.com/pola-rs/polars/) dataframe engine directly into the grammar of Nushell commands, but this left many rough edges. Thus [with version 0.76, we returned to a simpler dataframe integration](https://www.nushell.sh/blog/2023-02-21-nushell_0_76.html#dataframe-commands-are-again-explicitly-separated-from-core-nushell-commands-7998) to focus on getting the core Nushell experience right.
4141

42-
This all provided valuable lessons in how we decide which features are beginning to settle and where we need to focus our effort to get a consistent experience.
42+
This all provided valuable lessons in how we decide which features are beginning to settle and where we need to focus our effort to get a consistent experience.
4343

4444
## What we are working on right now
4545

@@ -66,7 +66,7 @@ Achieving stability will not be possible without all of you that have dedicated
6666

6767
We want to incorporate you in formulating the priorities for the 1.0 release by reaching out to folks interested in particular problems to get clearer roadmaps for specific areas. Those write-ups will hopefully also provide some inspiration for folks interested in helping out and pushing Nushell forward.
6868

69-
But setting our sights on reaching the stable 1.0 release will also impose some limitations on our development practices. We are now much less likely to accept new features, commands, or options as they need to work well together with the larger picture.
69+
But setting our sights on reaching the stable 1.0 release will also impose some limitations on our development practices. We are now much less likely to accept new features, commands, or options as they need to work well together with the larger picture.
7070
A lot of effort will still need to go into cleaning up the internals and fixing bugs. As we want to systematically improve our binary size, compile times, and runtime performance, improving existing algorithms and paying back technical debt will be prioritized over experimental stuff.
7171
Any external dependencies will also come under much more scrutiny.
7272
This means, to reduce the total number of crates we will seek to replace redundant or mostly superfluous dependencies with fewer high-quality implementations and also severely restrict the addition of any new dependencies.

book/cheat_sheet.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@
412412

413413
> **custom command which takes any number of positional arguments using rest params**
414414
415-
## Variables & Subexpressions
415+
## Variables
416416

417417
```nu
418418
> let val = 42
@@ -475,10 +475,10 @@
475475

476476
```nu
477477
> let big_files = (ls | where size > 10kb)
478-
> $big_files
478+
> $big_files
479479
```
480480

481-
> **using subexp­ression by wrapping the expression with parent­heses ()**
481+
> **assigning the result of a pipeline to a variable**
482482
483483
---
484484

book/control_flow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Nushell provides several commands that help determine how different groups of code are executed. In programming languages this functionality is often referred to as _control flow_.
44

55
::: tip
6-
One thing to note is that all of the commands discussed on this page use [blocks](/book/types_of_data.html#blocks). This means you can mutate [environmental variables](/book/environment.html) and other [mutable variables](http://localhost:8080/book/variables_and_subexpressions.html#mutable-variables) in them.
6+
One thing to note is that all of the commands discussed on this page use [blocks](/book/types_of_data.html#blocks). This means you can mutate [environmental variables](/book/environment.html) and other [mutable variables](/book/variables.html#mutable-variables) in them.
77
:::
88

99
## Already covered

book/environment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Individual environment variables are fields of a record that is stored in the `$
8080
BAR
8181
```
8282

83-
Sometimes, you may want to access an environmental variable which might be unset. Consider using the [question mark operator](variables_and_subexpressions.md#variable-paths) to avoid an error:
83+
Sometimes, you may want to access an environmental variable which might be unset. Consider using the [question mark operator](types_of_data.md#optional-cell-paths) to avoid an error:
8484
```nu
8585
> $env.FOO | describe
8686
Error: nu::shell::column_not_found

book/pipelines.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ The `$in` variable will collect the pipeline into a value for you, allowing you
2525

2626
## Multi-line pipelines
2727

28-
If a pipeline is getting a bit long for one line, you can enclose it within `(` and `)` to create a subexpression:
28+
If a pipeline is getting a bit long for one line, you can enclose it within parentheses `()`:
2929

3030
```nu
31-
(
31+
let year = (
3232
"01/22/2021" |
3333
parse "{month}/{day}/{year}" |
3434
get year
3535
)
3636
```
3737

38-
Also see [Subexpressions](https://www.nushell.sh/book/variables_and_subexpressions.html#subexpressions)
39-
4038
## Semicolons
4139

4240
Take this example:

book/programming_in_nu.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Nushell's aliases work in a similar way and are a part of the programming langua
1111
Common operations can, such as addition or regex search, be done with [operators](operators.md).
1212
Not all operations are supported for all data types and Nushell will make sure to let you know.
1313

14-
You can store intermediate results to [variables](variables_and_subexpressions.md) and immediately evaluate subroutines with [subexpressions](variables_and_subexpressions.html#subexpressions).
14+
You can store intermediate results to [variables](variables.md).
15+
Variables can be immutable, mutable, or a parse-time constant.
1516

1617
The last three sections are aimed at organizing your code:
1718

book/stdout_stderr_exit_codes.md

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ Without the pipeline, Nushell will not do any redirection, allowing it to print
2020

2121
Another common stream that external applications often use to print error messages is stderr. By default, Nushell does not do any redirection of stderr, which means that by default it will print to the screen.
2222

23-
You can force Nushell to do a redirection by using `do { ... } | complete`. For example, if we wanted to call the external above and redirect its stderr, we would write:
24-
25-
```nu
26-
> do { external } | complete
27-
```
28-
2923
## Exit code
3024

3125
Finally, external commands have an "exit code". These codes help give a hint to the caller whether the command ran successfully.
@@ -37,7 +31,7 @@ Nushell tracks the last exit code of the recently completed external in one of t
3731
> $env.LAST_EXIT_CODE
3832
```
3933

40-
The second uses a command called [`complete`](/commands/docs/complete.md).
34+
The second way is to use the [`complete`](/commands/docs/complete.md) command.
4135

4236
## Using the [`complete`](/commands/docs/complete.md) command
4337

@@ -46,7 +40,7 @@ The [`complete`](/commands/docs/complete.md) command allows you to run an extern
4640
If we try to run the external `cat` on a file that doesn't exist, we can see what [`complete`](/commands/docs/complete.md) does with the streams, including the redirected stderr:
4741

4842
```nu
49-
> do { cat unknown.txt } | complete
43+
> cat unknown.txt | complete
5044
╭───────────┬─────────────────────────────────────────────╮
5145
│ stdout │ │
5246
│ stderr │ cat: unknown.txt: No such file or directory │
@@ -72,20 +66,59 @@ The log level for output can be set with the `NU_LOG_LEVEL` environment variable
7266
NU_LOG_LEVEL=DEBUG nu std_log.nu
7367
```
7468

75-
## Using `out>`, `err>` to redirect stdout and stderr to files
69+
## File redirections
7670

77-
If you want to redirect output to file, you can just type something like this:
71+
If you want to redirect stdout of an external command to a file, you can use `out>` followed by a file path. Similarly, you can use `err>` to redirect stderr:
7872

7973
```nu
8074
cat unknown.txt out> out.log err> err.log
8175
```
8276

83-
If you want to redirect both stdout and stderr to the same file, just type something like this:
77+
If you want to redirect both stdout and stderr to the same file, you can use `out+err>`:
8478

8579
```nu
8680
cat unknown.txt out+err> log.log
8781
```
8882

83+
Note that `out` can be shortened to just `o`, and `err` can be shortened to just `e`. So, the following examples are equivalent to the previous ones above:
84+
```nu
85+
cat unknown.txt o> out.log e> err.log
86+
87+
cat unknown.txt o+e> log.log
88+
```
89+
90+
Also, any expression can be used for the file path, as long as it is a string value:
91+
```nu
92+
use std
93+
cat unknown.txt o+e> (std null-device)
94+
```
95+
96+
Note that file redirections are scoped to an expression and apply to all external commands in the expression. In the example below, `out.txt` will contain `hello\nworld`:
97+
```nu
98+
let text = "hello\nworld"
99+
($text | head -n 1; $text | tail -n 1) o> out.txt
100+
```
101+
Pipes and additional file redirections inside the expression will override any file redirections applied from the outside.
102+
103+
## Pipe redirections
104+
105+
If a regular pipe `|` comes after an external command, it redirects the stdout of the external command as input to the next command. To instead redirect the stderr of the external command, you can use the stderr pipe, `err>|` or `e>|`:
106+
107+
```nu
108+
cat unknown.txt e>| str upcase
109+
```
110+
111+
Of course, there is a corresponding pipe for combined stdout and stderr, `out+err>|` or `o+e>|`:
112+
113+
```nu
114+
nu -c 'print output; print -e error' o+e>| str upcase
115+
```
116+
117+
Unlike file redirections, pipe redirections do not apply to all commands inside an expression. Rather, only the last command in the expression is affected. For example, only `cmd2` in the snippet below will have its stdout and stderr redirected by the pipe.
118+
```nu
119+
(cmd1; cmd2) o+e>| cmd3
120+
```
121+
89122
## Raw streams
90123

91124
Both stdout and stderr are represented as "raw streams" inside of Nushell. These are streams of bytes rather than structured data, which are what internal Nushell commands use.

book/table_of_contents.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- [Custom commands](custom_commands.md) - Creating your own commands
1616
- [Aliases](aliases.md) - How to alias commands
1717
- [Operators](operators.md) - Operators supported by Nushell
18-
- [Variables and subexpressions](variables_and_subexpressions.md) - Working with variables and working with subexpressions
18+
- [Variables](variables.md) - Working with variables
1919
- [Control flow](control_flow.md) - Working with the control flow commands
2020
- [Environment](environment.md) - Working with environment variables
2121
- [Stdout, stderr, and exit codes](stdout_stderr_exit_codes.md) - Working with stdout, stderr, and exit codes

book/thinking_in_nu.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Another common issue is trying to dynamically create the filename to source from
5151
This doesn't work if `my_path` is a regular runtime variable declared with `let`. This would require the
5252
evaluator to run and evaluate the string, but unfortunately Nushell needs this information at compile-time.
5353

54-
However, if `my_path` is a [constant](/book/variables_and_subexpressions#constant-variables), then this
54+
However, if `my_path` is a [constant](/book/variables#constant-variables), then this
5555
would work, since the string can be evaluated at compile-time:
5656

5757
```nu

book/types_of_data.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ Moreover, you can also access entire columns of a table by name, to obtain lists
404404
╰───┴────╯
405405
```
406406

407-
Of course, these resulting lists don't have the column names of the table. To remove columns from a table while leaving it as a table, you'll commonly use the [`select`](/commands/docs/select.md) command with column names:
407+
Of course, these resulting lists don't have the column names of the table. To choose columns from a table while leaving it as a table, you'll commonly use the [`select`](/commands/docs/select.md) command with column names:
408408

409409
```nu
410410
> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z
@@ -417,7 +417,7 @@ Of course, these resulting lists don't have the column names of the table. To re
417417
╰───┴───┴───╯
418418
```
419419

420-
To remove rows from a table, you'll commonly use the [`select`](/commands/docs/select.md) command with row numbers, as you would with a list:
420+
To get specific rows from a table, you'll commonly use the [`select`](/commands/docs/select.md) command with row numbers, as you would with a list:
421421

422422
```nu
423423
> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2

book/variables.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Variables
2+
3+
Nushell values can be assigned to named variables using the `let`, `const`, or `mut` keywords.
4+
After creating a variable, we can refer to it using `$` followed by its name.
5+
6+
## Types of Variables
7+
8+
### Immutable Variables
9+
10+
An immutable variable cannot change its value after declaration. They are declared using the `let` keyword,
11+
12+
```nu
13+
> let val = 42
14+
> print $val
15+
42
16+
```
17+
18+
However, they can be 'shadowed'. Shadowing means that they are redeclared and their initial value cannot be used anymore within the same scope.
19+
20+
```nu
21+
> let val = 42 # declare a variable
22+
> do { let val = 101; $val } # in an inner scope, shadow the variable
23+
101
24+
> $val # in the outer scope the variable remains unchanged
25+
42
26+
```
27+
28+
### Mutable Variables
29+
30+
A mutable variable is allowed to change its value by assignment. These are declared using the `mut` keyword.
31+
32+
```nu
33+
> mut val = 42
34+
> $val += 27
35+
> $val
36+
69
37+
```
38+
39+
There are a couple of assignment operators used with mutable variables
40+
41+
| Operator | Description |
42+
| -------- | -------------------------------------------------------------------------- |
43+
| `=` | Assigns a new value to the variable |
44+
| `+=` | Adds a value to the variable and makes the sum its new value |
45+
| `-=` | Subtracts a value from the variable and makes the difference its new value |
46+
| `*=` | Multiplies the variable by a value and makes the product its new value |
47+
| `/=` | Divides the variable by a value and makes the quotient its new value |
48+
| `++=` | Appends a list or a value to a variable |
49+
50+
> **Note**
51+
>
52+
> 1. `+=`, `-=`, `*=` and `/=` are only valid in the contexts where their root operations
53+
> are expected to work. For example, `+=` uses addition, so it can not be used for contexts
54+
> where addition would normally fail
55+
> 2. `++=` requires that either the variable **or** the argument is a
56+
> list.
57+
58+
#### More on Mutability
59+
60+
Closures and nested `def`s cannot capture mutable variables from their environment. For example
61+
62+
```nu
63+
# naive method to count number of elements in a list
64+
mut x = 0
65+
66+
[1 2 3] | each { $x += 1 } # error: $x is captured in a closure
67+
```
68+
69+
To use mutable variables for such behaviour, you are encouraged to use the loops
70+
71+
### Constant Variables
72+
73+
A constant variable is an immutable variable that can be fully evaluated at parse-time. These are useful with commands that need to know the value of an argument at parse time, like [`source`](/commands/docs/source.md), [`use`](/commands/docs/use.md) and [`register`](/commands/docs/register.md). See [how nushell code gets run](how_nushell_code_gets_run.md) for a deeper explanation. They are declared using the `const` keyword
74+
75+
```nu
76+
const plugin = 'path/to/plugin'
77+
register $plugin
78+
```
79+
80+
## Variable Names
81+
82+
Variable names in Nushell come with a few restrictions as to what characters they can contain. In particular, they cannot contain these characters:
83+
84+
```
85+
. [ ( { + - * ^ / = ! < > & |
86+
```
87+
88+
It is common for some scripts to declare variables that start with `$`. This is allowed, and it is equivalent to the `$` not being there at all.
89+
90+
```nu
91+
> let $var = 42
92+
# identical to `let var = 42`
93+
```

0 commit comments

Comments
 (0)