Bash For Loop With Examples

Bash For Loop With Examples
Bash For Loop With Examples

Bash For Loop

Bash For Loop is used to execute a series of commands repeatedly until certain condition reached. Basically, Loops in any programming languages are used to execute a series of commands or tasks again and again until the certain condition becomes false. Loops are useful in bash to perform repetitive tasks. In this tutorial, you are going to learn Bash For Loop with continue and break statement.

Basic Syntax for Bash For Loop

For Loop in Bash has very similar syntax as in other programming languages. Following is the basic syntax for Bash For Loop.

for ITEM in LIST
do
   COMMANDS
done

Where,
LIST: This is basically a list of strings, an array or output of commands etc.
ITEM: It’s a single item in LIST.
COMMANDS: A set of commands to execute during the execution of For Loop.

For Loop to Print Series of Strings

You can use for loop in Bash to perform commands on the series of strings separated by spaces. In the following example, we are going to loop over a series of strings separated by spaces.

#!/bin/bash
    
# Basic for loop
cities="Tokyo London Paris Dubai Mumbai"
for city in $cities
do
    echo "CITY: $city"
done

The output should be:

CITY: Tokyo
CITY: London
CITY: Paris
CITY: Dubai
CITY: Mumbai

For Loop with Array Elements

By using For Loop we can iterate over array elements. It’s very easy to iterate over array elements. In the following example we are going to iterate over number ranges by using for loop:

#!/bin/bash

FRUITS=('Apple' 'Mango' 'Strawberry' 'Orange' 'Banana')

for item in "${FRUITS[@]}"; 
do
  echo "FRUIT: $item"
done

The output should be:

FRUIT: Apple
FRUIT: Mango
FRUIT: Strawberry
FRUIT: Orange
FRUIT: Banana

For Loop with Ranges

By using For Loop we can loop over number ranges by using expressions to create ranges of numbers or characters. In the following example we are going to iterate over number ranges by using for loop:

for i in {5..1}
do
  echo "COUNT: $i"
done

The output should be:

COUNT: 5
COUNT: 4
COUNT: 3
COUNT: 2
COUNT: 1

Bash For Loop With Condition Like C Language

You can also write a For Loop like C language using three expressions in Bash Scripting. In this type of loop three expressions are used for initialization, condition and increment operation. Following is the basic syntax for C style loops:

for (( INITIALIZATION_EXP; CONDITION_EXP; INCREMENT_EXP ))
do
   
done

Where,
INITIALIZATION_EXP: This expression is used to initialize a loop.
CONDITION_EXP: This type of expression is used for defining condition in for loop.
INCREMENT_EXP: It is used to increment the variable value.

Below is an example for Bash Loop with three expressions:

#!/bin/bash

# for loop for preinting 5 to 1 numbers 
for (( i=5; i>=1; i-- ))
do  
   echo "COUNT: $i"
done

The output should be:

COUNT: 5
COUNT: 4
COUNT: 3
COUNT: 2
COUNT: 1

Infinite Loop

Infinite loop in bash defined by using three expression C style For Loop. In the following example Infinite For Loop is defined:

#!/bin/bash

# infinite loop
for (( ; ; ))
do
   echo "Press Ctrl+C to stop this loop."
   echo "Infinite loop is executing..."
done

After executing the above loop you can stop and get out of this loop by using Ctrl+C.

Break And Continue Statements

Break and Continue statements in loops are used to control the execution. The break statement used to break the loop and get out of it. While Continue statement passes control of the loop to next iteration.

Break Statement

The break statement terminates the current loop and passes the control to the next statement. By using break statement you can terminate the loop when certain condition reaches.

Following is the basic example of For Loop using Break statement:

#!/bin/bash

for city in Tokyo London Paris Dubai Mumbai
do
  if [[ "$city" == 'Dubai' ]]; then
    break
  fi
  echo "CITY: $city"
done

echo "Loop Ended"

The output should be:

CITY: Tokyo
CITY: London
CITY: Paris
Loop Ended

Continue Statement

Continue statement passes the control of the loop to the next iteration of the loop. By using continue statement you can pass control to the next iteration of the loop when certain condition reaches.

Following is the basic example of For Loop using Break statement:

#!/bin/bash

for city in Tokyo London Paris Dubai Mumbai
do
  if [[ "$city" == 'Dubai' ]]; then
    continue
  fi
  echo "CITY: $city"
done

echo "Loop Ended"

The output should be:

CITY: Tokyo
CITY: London
CITY: Paris
CITY: Mumbai
Loop Ended

Bash For Loop Examples

In the following example, it will check firefox.desktop file is exists or not in applications/ directory.

#!/bin/bash

for file in /usr/share/applications/*
do
        if [ "${file}" == "/usr/share/applications/firefox.desktop" ]
        then
            echo "The file is found in applications directory";
        fi
done

bash for loop with examples - program
bash for loop with examples – program

The output should be (if firefox.desktop is present in given directory):

The file is found in applications directory
bash for loop with examples - output
bash for loop with examples – output

In the following example, it will rename files names whose names contain spaces.

#!/bin/bash

# for loop starts. it will iterate over all the files in current directory
for file in *\ *; do
    # it will replace spacesin file names
    mv "$file" "${file// /-}"
done

Conclusion

You have successfully learned the Bash For Loop with examples. If you have any of the queries regarding this then please don’t forget to comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here