For Statement: The First Form of For
Learn about the for statement in detail.
We'll cover the following...
For statement
There is another loop statement in Bash called **for**
. We should use for
when we know the number of iterations we need in advance.
The for
statement has two forms. The first one processes words in a string sequentially. The second form applies an arithmetic expression in the loop condition.
The first form of for
Let’s start with the first form of the for
statement. It looks like this in the general form:
for VARIABLE in STRING
do
ACTION
done
We can write the same construction in a single line like this:
for VARIABLE in STRING; do ACTION; done
The ACTION
of the for
statement is a single command or a block of commands. It is the same thing as the one in the while
statement.
Bash performs all expansions in the for
condition before starting the first iteration of the loop. What does it mean? Let’s suppose we specified the command instead of the STRING
. Then, Bash executes this command and replaces it with its ...