# The C-Style for Command - Shell Scripting

If you’ve done any programming using the C programming language, you’re probably surprised by the way the bash shell uses the for command. In the C language, a for loop normally defines a variable, which it then alters automatically during each iteration. Normally, programmers use this variable as a counter, and either increment or decrement the counter by one in each iteration.The bash for command can also provide this functionality. This section shows how you can usea C-style for command in a bash shell script.

The C language for command

The C language for command has a specific method for specifying a variable, a condition that must remain true for the iterations to continue, and a method for altering the variable for each iteration. When the specified condition becomes false, the for loop stops. The condition equationis defined using standard mathematical symbols. For example, take the C language code:

for (i = 0; i ‹ 10; i++)
{
printf("The next number is %d ", i);
}

This code produces a simple iteration loop, where the variable i is used as a counter. The first section assigns a default value to the variable. The middle section defines the condition under which the loop will iterate. When the defined condition becomes false, the for loop stops iterations.

The last section defines the iteration process. After each iteration, the expression defined in the last section is executed. In this example, the i variable is incremented by one after each iteration.

The bash shell also supports a version of the for loop that looks similar to the C-style for loop, although it does have some subtle differences, including a couple of things that’ll confuse shell script programmers. Here’s the basic format of the C-style bash for loop:

for (( variable assignment condition iteration process ))

The format of the C-style for loop can be confusing for bash shell script programmers, as it usesC-style variable references instead of the shell-style variable references. Here’s what a C-style for command looks like:

for (( a = 1; a ‹ 10; a++ ))
Notice that there are a couple of things that don’t follow the standard bash shell for method:

• The assignment of the variable value can contain spaces.
• The variable in the condition isn’t preceded with a dollar sign.
• The equation for the iteration process doesn’t use the expr command format.

The shell developers created this format to more closely resemble the C-style for command. While this is great for C programmers, it can throw even expert shell programmers into a tizzy. Be careful when using the C-style for loop in your scripts. Here’s an example of using the C-style for command in a bash shell program:

$cat test8 #!/bin/bash # testing the C-style for loop for (( i=1; i ‹= 10; i++ )) do echo "The next number is$i"
done
$./test8 The next number is 1 The next number is 2 The next number is 3 The next number is 4 The next number is 5 The next number is 6 The next number is 7 The next number is 8 The next number is 9 The next number is 10$

The for loop iterates through the commands using the variable defined in the for loop (the letteri in this example). In each iteration, the $i variable contains the value assigned in the for loop. After each iteration, the loop iteration process is applied to the variable, which in this example, increments the variable by one. Using multiple variables The C-style for command also allows you to use multiple variables for the iteration. The loop handles each variable separately, allowing you to define a different iteration process for each variable.While you can have multiple variables, you can only define one condition in the for loop:$ cat test9
#!/bin/bash
# multiple variables
for (( a=1, b=10; a ‹= 10; a++, b-- ))
do
echo "$a -$b"
done
$./test9 1 - 10 2 - 9 3 - 8 4 - 7 5 - 6 6 - 5 7 - 4 8 - 3 9 - 2 10 - 1$

The a and b variables are each initialized with different values, and different iteration processes are defined. While the loop increases the a variable, it decreases the b variable for each iteration.