Bash For and While Loop Examples


Bash can provide different programming language structures like a variable, array, loop, decision making, etc. Loop is one of the most useful features of bash scripting. Loops will give the ability to iterate over given sequential structures like an array, file list, numbers, inputs, etc.

Loop Syntax

Bash provides alternative syntax for a loop. Below are some examples of for loop syntax.

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

OR

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

OR

for OUTPUT in $(Linux-Or-Unix-Command-Here)
do
 command1 on $OUTPUT
 command2 on $OUTPUT
 commandN
done

For line provides the loop logic and take a step for each number or file or command output. Lines between do and done is the part which will run each step.

Loop Items

One of the most popular usages of the loop is looping through items. We will provide items by separating them with space. All items will be looped. In this example, we will use numbers from 1 to 7 and define them one by one.

$ for count in 1 2 3 4 5 6 7 ; do echo $count; done
Loop Numbers
Loop ITems

Loop Range

We can loop numbers like above but the problem is how can we achieve if need to loop over 1000 numbers. Should we write the one by one? No, we can specify number ranges like below.

$ for count in {1..7} ; do echo $count; done

We use {1..7} to loop from 1 to 7 one by one.

Increment Value

We can also specify the increment number. Incrementing one by one is the default behavior. How can we increment 2?

$ for count in {1..5..2} ; do echo $count; done
Increment Value
Increment Value

We have incremented loop 2 and only used numbers 1,3,5. There is an alternative way to increment for loop with 43 for statement

LEARN MORE  Awk Print Tutorial With Examples

C Style For Loop

We can specify a statement like the C programming language. We will set stat numbers, end condition and increment value like below. Here is the syntax

for (( EXP1; EXP2; EXP3 ))
do
 command1
 ...
done

Now we will start at 1 and increment one by one to the 5.

$ for (( count=1;count<6;count++)) ; do echo $count; done
C Style For Loop
C Style For Loop

Infinite For Loop

We can create a loop for infinity. This loop will run forever if we do not end the process.

$ for (( ; ; )) ; do echo "Kill me if you can ;)"; done

We do not provide any start number, end condition or increment value to trigger an infinite loop.

Infinite While Loop

We can create infinite loop with a while statement. Creating an infinite loop while statement is more easy.

$ while true ; do echo "Kill me if you can ;)"; done

Just provide a true statement to the while.

Loop Through Files

One of the most used types of loop is looping through files in the specified directory. All filenames in the specified directory will be looped step by step.

$ for file in $(ls -1) ; do echo $file; done
Loop Through Files
Loop Through Files

Here the $(ls -l) provides a list of filenames to loop and each file names is assigned to the file variable for each step.

1 thought on “Bash For and While Loop Examples”

Leave a Comment