in

LinuxLinux

Bash For Loop Command & Examples Ubuntu 18.04, 19.04 & Debian Distros

The Essential Bash For Loop Examples & Command!!

Bash For Loop

B

ash For Loop: Loops are the basic function concept in any programming Languages. This types of Loops are mainly used to do a repeating task for a time or iteration. There are three types of loops are available in programming and they are :

  • For Loop
  • Do Loop
  • Do-While Loop

In scripting languages like Bash, Loops are the most useful function to do the repeated task. Loop will carry another function and repeat the function for a repeated time until the iteration came to an end. In this article, We are going to explain about the Bash For Loop Syntax, Commands & Examples for Ubuntu and Debian based Distros.

Jump To:

Basic Syntax of Bash of Loop:

The basic syntax of “Bash For Loop” is listed below:

Syntax:

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

You can also use the following syntax:

Syntax:

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

Check this too  fping- Best Ping tool for Linux & its Installation on Ubuntu

You can also use the following syntax:

Syntax:

for OUTPUT in $(You command here!!)
do
command1 on $OUTPUT
                command2 on $OUTPUT
                .
                .
                commandN
done

Using Loop for Infinite Iteration:

You can use the loop for infinite iteration process, To do an infinite iteration function use the following command:

#tecrobust!/bin/bash
for (( ; ; ))
do
echo “infinite loops [ Press CTRL+C to stop]”
done

The above command will help you to do an infinite iteration.

Early Conditional Exit & Break:

You can exit the iteration using the “Break” command before the iteration came to an end. This command can be used for all loops such as ” For, Do & Do-While “.

Code
for I in 1 2 3 4 5
do
statement1                                  #Condition with iteration I
statement2
if (disaster-condition)
then
break                                          #Break the Loop.
fe
statement3                                  #Another Condition.
done

Early Continuation with continue statement:

The below syntax is mainly used to continue the next iteration of the enclosing using the “continue” command:

Code
for I in 1 2 3 4 5
do
statement1        #The conditions are executed under the Iteration values.
statement2
if (condition)
then
continue             #Continues the Next Iteration
   fe
statement3
done

For Loop Over a String:

You can use the For Loop to iterate over each item in the list of strings. The below command will help you understand more about Loop over a string concept.

Code
for utensils in Pen Paper Pencil Ruler
do
echo “Elements: $utensils
done

The output of the above command is:

Output
Elements: Pen
Elements: Paper
Elements: Pencil
Elements: Ruler

For Loop over a Number Range:

You can use the loop to iterate the loop over a number range using the sequence expression. You need to define the starting point and the ending point of the range of the numbers.

Check this too  20+ CURL Linux Commands 2019 For Ubuntu 18.04 & All Linux Distros

The syntax of the Sequence Expression is:

Code
{START..END}

The example command for Loop over a Number Range:

Code
for i in {0..5}
do
echo “Number: $i
done

The output of the above command is:

Output
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

For Loop Over Array Elements:

You can use the For loop to iterate over array elements. The below command will help you understand how the For Loop over array elements works:

Code
PLANETS=(‘Mercury’ ‘Venus’ ‘Earth’ ‘Jupiter’)
for planets in “${[email protected]]}”;
   do
            echoPLANETS: $planets
  done

The output of the above command is:

Output
PLANETS: Mercury
PLANETS: Venus
PLANETS: Earth
PLANETS: Jupiter

Bash For Loop Examples:

How to Replace space with Underscore using For Loop:

You can use the For Loop to change the replace the spaces with underscore. The below command will help you replace the spaces into underscore.

Code

for files in *\ *;

do
mv “$files” “${files// /_}
done

  • *\ *– Creates the list
  • ${file// /_}– Replaces the string within a parameter using a pattern. The command will replaces the spaces with underscore
  • done– Which ends the loop statement.

Change the File Extensions using For Loop:

You can use the For loop to change the file extensions. For example, You can change the file extension “.jpeg to .jpg“. The below command will help you understand more about this.

Code

for files in *.jpeg;

do
mv — “$files” “${file%.jpeg}.jpg”
done

Check this too  Commands To Remove Unused Packages & Get Linux Server IP Address

The above command will help you change the file extension.

  • for file in *.jpeg– Create the iteration.
  • ${file%.jpeg}– Changing the file extension with a pattern
  • done– Which completes the iteration.

Verdict:

That’s it. We hope that you learned some code and tricks here. In this article, We deeply discussed the topic “Bash For Loop” in Linux. If you have any query and then feel free to comment us.

Article Review
  • Content Explanation
  • Command Syntax
  • Example for Command
  • List of Usage
4.8

Summary

For more article, Kindly Visit our HomePage

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@] gmail.com
Sub: Message to Julian

Is It Down For ME.Net

Check Website Up or Down using Is It Down For Me.Net Online Tool

telegram application

Telegram Ubuntu- Install Telegram desktop for Ubuntu 18.04 LTS and 19.04