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:

for ((i=1; i <= MAX ; i++)) ; do
	echo "$i"

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


ARRAY=( 'Debian Linux' 'Redhat Linux' 'Ubuntu Linux' )

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

Comments and Discussions
Linux Forum