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


Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top