for
for core
Loop over a range.
Signature
> for (var_name) (range) (block) --numbered
Parameters
var_name
: name of the looping variablerange
: range of the loopblock
: the block to run--numbered
(-n)
: return a numbered item ($it.index and $it.item)
Input/output types:
input | output |
---|---|
nothing | nothing |
Examples
Echo the square of each integer
> for x in [1 2 3] { print ($x * $x) }
Work with elements of a range
> for $x in 1..3 { print $x }
Number each item and echo a message
> for $it in ['bob' 'fred'] --numbered { 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