While Statement
Learn about the while operator in detail.
We'll cover the following...
while
Bash provides two loop operators: while
and for
. We will start with the **while**
statement because it is more straightforward than for
.
The while
syntax resembles the if
statement. If we write while
in the general form, it looks like this:
while CONDITION
do
ACTION
done
We can write the while
statement in one line:
while CONDITION; do ACTION; done
Both CONDITION
and ACTION
can be a single command or block of commands. The ACTION
is called the loop body.
When Bash executes the while
loop, it checks the CONDITION
first. If a command of the CONDITION
returns the zero exit status, it means “true.” Bash executes the ACTION
in the loop body in this case. Then, it rechecks the CONDITION
. If it still equals “true,” the ACTION
is performed again. The loop execution stops when the CONDITION
...