Bash Control Structures For Loop with C-style syntax

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any Bash Question?