Home Explore Blog CI



nushell

commands/docs/collect.md
692454e0981d0e7cf59cdf7939375b099b502158acb105390000000300000451
---
title: collect
categories: |
  filters
version: 0.104.0
filters: |
  Collect a stream into a value.
usage: |
  Collect a stream into a value.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

# `collect` for [filters](/commands/categories/filters.md)

<div class='command-title'>Collect a stream into a value.</div>

## Signature

```> collect {flags} (closure)```

## Flags

 -  `--keep-env`: let the closure affect environment variables

## Parameters

 -  `closure`: The closure to run once the stream is collected.


## Input/output types:

| input | output |
| ----- | ------ |
| any   | any    |
## Examples

Use the second value in the stream
```nu
> [1 2 3] | collect { |x| $x.1 }
2
```

Read and write to the same file
```nu
> open file.txt | collect | save -f file.txt

```

## Notes
If provided, run a closure with the collected value as input.

The entire stream will be collected into one value in memory, so if the stream
is particularly large, this can cause high memory usage.

Chunks
77cfb05d (1st chunk of `commands/docs/collect.md`)
Title: collect
Summary
The `collect` command in Nushell gathers a stream of data into a single value, optionally processing it with a closure. It's useful for extracting specific elements or performing operations on the entire stream at once. Be mindful of memory usage with large streams.