2022-02-14 02:22:51 +00:00
|
|
|
---
|
|
|
|
title: for
|
|
|
|
layout: command
|
|
|
|
version: 0.59.0
|
|
|
|
---
|
2021-05-30 00:57:04 +00:00
|
|
|
|
2022-02-14 02:22:51 +00:00
|
|
|
Loop over a range
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```> for (var_name) (range) (block) --numbered```
|
2021-05-30 00:57:04 +00:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
2022-02-14 02:22:51 +00:00
|
|
|
- `var_name`: name of the looping variable
|
|
|
|
- `range`: range of the loop
|
|
|
|
- `block`: the block to run
|
|
|
|
- `--numbered`: returned a numbered item ($it.index and $it.item)
|
2021-05-30 00:57:04 +00:00
|
|
|
|
|
|
|
## Examples
|
2022-02-14 02:22:51 +00:00
|
|
|
|
|
|
|
Echo the square of each integer
|
2021-05-30 00:57:04 +00:00
|
|
|
```shell
|
|
|
|
> for x in [1 2 3] { $x * $x }
|
2022-02-14 02:22:51 +00:00
|
|
|
```
|
2021-05-30 00:57:04 +00:00
|
|
|
|
2022-02-14 02:22:51 +00:00
|
|
|
Work with elements of a range
|
2021-05-30 00:57:04 +00:00
|
|
|
```shell
|
|
|
|
> for $x in 1..3 { $x }
|
2022-02-14 02:22:51 +00:00
|
|
|
```
|
2021-05-30 00:57:04 +00:00
|
|
|
|
2022-02-14 02:22:51 +00:00
|
|
|
Number each item and echo a message
|
2021-05-30 00:57:04 +00:00
|
|
|
```shell
|
|
|
|
> for $it in ['bob' 'fred'] --numbered { $"($it.index) is ($it.item)" }
|
2022-02-14 02:22:51 +00:00
|
|
|
```
|
2021-05-30 00:57:04 +00:00
|
|
|
|