2022-02-14 03:22:51 +01:00
|
|
|
---
|
|
|
|
title: for
|
|
|
|
layout: command
|
2022-03-04 13:10:09 +01:00
|
|
|
version: 0.59.1
|
2022-02-14 03:22:51 +01:00
|
|
|
---
|
2021-05-30 02:57:04 +02:00
|
|
|
|
2022-02-14 03:22:51 +01:00
|
|
|
Loop over a range
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```> for (var_name) (range) (block) --numbered```
|
2021-05-30 02:57:04 +02:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
2022-02-14 03:22:51 +01: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 02:57:04 +02:00
|
|
|
|
|
|
|
## Examples
|
2022-02-14 03:22:51 +01:00
|
|
|
|
|
|
|
Echo the square of each integer
|
2021-05-30 02:57:04 +02:00
|
|
|
```shell
|
|
|
|
> for x in [1 2 3] { $x * $x }
|
2022-02-14 03:22:51 +01:00
|
|
|
```
|
2021-05-30 02:57:04 +02:00
|
|
|
|
2022-02-14 03:22:51 +01:00
|
|
|
Work with elements of a range
|
2021-05-30 02:57:04 +02:00
|
|
|
```shell
|
|
|
|
> for $x in 1..3 { $x }
|
2022-02-14 03:22:51 +01:00
|
|
|
```
|
2021-05-30 02:57:04 +02:00
|
|
|
|
2022-02-14 03:22:51 +01:00
|
|
|
Number each item and echo a message
|
2021-05-30 02:57:04 +02:00
|
|
|
```shell
|
|
|
|
> for $it in ['bob' 'fred'] --numbered { $"($it.index) is ($it.item)" }
|
2022-02-14 03:22:51 +01:00
|
|
|
```
|