Home Explore Blog CI



nushell

commands/docs/for.md
1aaf42ca2ab786edd3ab0555b53a7a7bbe0ec5743b75876f0000000300000427
---
title: for
categories: |
  core
version: 0.104.0
core: |
  Loop over a range.
usage: |
  Loop over a range.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

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

<div class='command-title'>Loop over a range.</div>

## Signature

```> for {flags} (var_name) (range) (block)```

## Parameters

 -  `var_name`: Name of the looping variable.
 -  `range`: Range of the loop.
 -  `block`: The block to run.


## Input/output types:

| input   | output  |
| ------- | ------- |
| nothing | nothing |
## Examples

Print the square of each integer
```nu
> for x in [1 2 3] { print ($x * $x) }

```

Work with elements of a range
```nu
> for $x in 1..3 { print $x }

```

Number each item and print a message
```nu
> for $it in (['bob' 'fred'] | enumerate) { print $"($it.index) is ($it.item)" }

```

## Notes
This command is a parser keyword. For details, check:
  https://www.nushell.sh/book/thinking_in_nu.html

Chunks
0008ca83 (1st chunk of `commands/docs/for.md`)
Title: for command in Nushell
Summary
The `for` command in Nushell allows you to loop over a range of values, assigning each value to a variable and executing a block of code. It takes a variable name, a range, and a block as input. Examples include printing the square of each integer in a range, iterating over elements of a range, and using `enumerate` to number each item and print a message.