date now # 1: today
| $in + 1day # 2: tomorrow
| format date '%F' # 3: Format as YYYY-MM-DD
| $'($in) Report' # 4: Format the directory name
| mkdir $in # 5: Create the directory
```
While the second form may be overly verbose for this contrived example, you'll notice several advantages:
- It can be composed step-by-step with a simple <kbd>↑</kbd> (up arrow) to repeat the previous command and add the next stage of the pipeline.
- It's arguably more readable.
- Each step can, if needed, be commented.
- Each step in the pipeline can be [`inspect`ed for debugging](/commands/docs/inspect.html).
Let's examine the contents of `$in` on each line of the above example:
- On line 2, `$in` refers to the results of line 1's `date now` (a datetime value).
- On line 4, `$in` refers to tomorrow's formatted date from line 3 and is used in an interpolated string
- On line 5, `$in` refers to the results of line 4's interpolated string, e.g. '2024-05-14 Report'
### Pipeline Input in Filter Closures
Certain [filter commands](/commands/categories/filters.html) may modify the pipeline input to their closure in order to provide more convenient access to the expected context. For example:
```nu
1..10 | each {$in * 2}
```
Rather than referring to the entire range of 10 digits, the `each` filter modifies `$in` to refer to the value of the _current iteration_.
In most filters, the pipeline input and its resulting `$in` will be the same as the closure parameter. For the `each` filter, the following example is equivalent to the one above:
```nu
1..10 | each {|value| $value * 2}
```
However, some filters will assign an even more convenient value to their closures' input. The `update` filter is one example. The pipeline input to the `update` command's closure (as well as `$in`) refers to the _column_ being updated, while the closure parameter refers to the entire record. As a result, the following two examples are also equivalent:
```nu
ls | update name {|file| $file.name | str upcase}
ls | update name {str upcase}
```
With most filters, the second version would refer to the entire `file` record (with `name`, `type`, `size`, and `modified` columns). However, with `update`, it refers specifically to the contents of the _column_ being updated, in this case `name`.
### Pipeline Input in Custom Command Definitions and Scripts
See: [Custom Commands -> Pipeline Input](custom_commands.html#pipeline-input)
### When Does `$in` Change (and when can it be reused)?
- **_Rule 1:_** When used in the first position of a pipeline in a closure or block, `$in` refers to the pipeline (or filter) input to the closure/block.
Example:
```nushell
def echo_me [] {
print $in
}
true | echo_me
# => true
```
- **_Rule 1.5:_** This is true throughout the current scope. Even on subsequent lines in a closure or block, `$in` is the same value when used in the first position of _any pipeline_ inside that scope.
Example:
```nu
[ a b c ] | each {
print $in
print $in
$in
}
```
All three of the `$in` values are the same on each iteration, so this outputs:
```nu
a
a
b
b
c
c
╭───┬───╮
│ 0 │ a │
│ 1 │ b │
│ 2 │ c │
╰───┴───╯
```
- **_Rule 2:_** When used anywhere else in a pipeline (other than the first position), `$in` refers to the previous expression's result:
Example:
```nushell
4 # Pipeline input
| $in * $in # $in is 4 in this expression
| $in / 2 # $in is now 16 in this expression
| $in # $in is now 8
# => 8
```
- **_Rule 2.5:_** Inside a closure or block, Rule 2 usage occurs inside a new scope (a sub-expression) where that "new" `$in` value is valid. This means that Rule 1 and Rule 2 usage can coexist in the same closure or block.
Example:
```nushell
4 | do {
print $in # closure-scope $in is 4
let p = ( # explicit sub-expression, but one will be created regardless