Forrest logo
back to the for tool

for:tldr:c79fc

for: Iterate over a given range of numbers.
$ for ${variable} in ${{from}..${to}..${step}}; do ${echo "Loop is executed"}; done
try on your machine

This command is a bash shell script using a for loop to iterate over a range of values defined by the variables ${from}, ${to}, and ${step}.

Here's how it breaks down:

  • for ${variable} in ${{from}..${to}..${step}}; do is the start of the loop declaration. It assigns a value to the variable ${variable} for each iteration, going from ${from} to ${to} with a step size of ${step}. These variables are inside double curly braces {{}} to facilitate the substitution of variable values.

  • ${echo "Loop is executed"} is the body of the loop. It denotes the actions to be performed within each iteration. In this case, it just prints the message "Loop is executed". The $ before echo ensures the command is evaluated.

  • done marks the end of the loop declaration.

To provide a clearer example, let's assume the following variable values:

${from} = 1, ${to} = 10, and ${step} = 2.

The loop will execute three times because it starts at 1 and goes up to 10 with a step of 2. The value of ${variable} will be 1, 3, and 5 respectively in each iteration.

For each iteration, the loop body will execute the command echo "Loop is executed" which will print the message "Loop is executed" on the console.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the for tool