Bash Control Structures For Loop with C-style syntax


Example

The basic format of C-style for loop is:

for (( variable assignment; condition; iteration process ))

Notes:

  • The assignment of the variable inside C-style for loop can contain spaces unlike the usual assignment
  • Variables inside C-style for loop aren't preceded with $.

Example:

for (( i = 0; i < 10; i++ ))
do
    echo "The iteration number is $i"
done

Also we can process multiple variables inside C-style for loop:

for (( i = 0, j = 0; i < 10; i++, j = i * i ))
do
    echo "The square of $i is equal to $j"
done