bash script loop code example

Example 1: bash for loop

#!/bin/bash
for (( c=1; c<=5; c++ ))
do  
   echo "Welcome $c times"
done

Example 2: bash for loop

for VARIABLE in 1 2 3 4 5 .. N
do
	command1
	command2
	commandN
done

Example 3: bash for loop

for VARIABLE in file1 file2 file3
do
	command1 on $VARIABLE
	command2
	commandN
done

Example 4: bash script loop

while [ <some test> ]
do
<commands>
done

Example 5: bash for loop

for FILE in $(ls -A); do la $FILE; done

Example 6: loop bash

Use for in bash for iterating words in a string or values in an array as:
for value in {1, 2, 3}; do echo $value; done
for value in $(cat arguments_files.txt); do [some_command]; done
And use while for iterating lines from a pipe output as:
cat arguments_file.txt | while read line; do [some_command]; done

Tags:

Misc Example