Writing a C style bash for loop – example

If you are stubborn C programmer and wish to get your way when using BASH you will be happy to know that BASH offers C style syntax for writing for loops. Below you can find two examples of C style bash for loop:

Simple c-style bash for look with three iterations:

#!/bin/bash
MAX=3
for ((i=1; i <= MAX ; i++)) ; do
	echo "$i"
done



c-style for loop used to print all array elements:

#!/bin/bash

ARRAY=( 'Debian Linux' 'Redhat Linux' 'Ubuntu Linux' )
ELEMENTS=${#ARRAY[@]}

for (( i=0;i<$ELEMENTS;i++)); do
    echo ${ARRAY[${i}]}
done


Comments and Discussions
Linux Forum