Forrest logo
back to the for tool

shell:warp:d869f

Shell for-loop
$ for ${variable} in ${sequence}; do
$ ${command}
$ done
try on your machine

This command is a syntax for a loop in shell scripting. It is used to repeatedly execute a set of commands for each item in a given sequence.

Here's how it works:

  1. The loop begins with the for keyword, followed by a variable name (e.g., ${variable}).
  2. The variable is assigned each successive value from the given sequence (e.g., ${sequence}).
  3. The do keyword marks the start of the commands that will be executed in each iteration of the loop.
  4. The ${command} represents the set of commands that will be executed for each item in the sequence.
  5. The done keyword marks the end of the loop.

In each iteration, the ${command} will be executed with the current value of ${variable}. After the commands are executed, the loop proceeds to the next item in the sequence, and the process repeats until all items have been processed.

For example, let's say we want to print the numbers 1 to 5. We can use the following command:

for num in 1 2 3 4 5; do
    echo $num
done

This will output:

1
2
3
4
5

In this case, the variable num is assigned each value from the sequence "1 2 3 4 5", and the echo command is executed for each value, resulting in the numbers being printed.

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