![Forrest logo](/images/logo.png)
shell:warp:d869f
Shell for-loop
$ for ${variable} in ${sequence}; do
$ ${command}
$ done
$ ${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:
- The loop begins with the
for
keyword, followed by a variable name (e.g.,${variable}
). - The variable is assigned each successive value from the given sequence (e.g.,
${sequence}
). - The
do
keyword marks the start of the commands that will be executed in each iteration of the loop. - The
${command}
represents the set of commands that will be executed for each item in the sequence. - 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.