Bash Loops

Examples of various looping scripts using bash

Arrays and Counts

Loop through an incremental count

for INDEX in {0..5}; do
echo $INDEX
done

# one liner
for I in {0..5}; do echo $INDEX; done

 

Files

Looping through items in a file

Using cat

for ITEM in `cat FILE.NAME`; 
do
   echo $ITEM
done

#one liner
for ITEM in `cat FILE.NAME`; do echo $ITEM; done

Redirecting the contents

while read ITEM; do
echo $ITEM
done < FILE.NAME

 

Sidebar