• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash For Loop

Written by Admin, Updated On October 11, 2020
bash, loop, terminal
Bash For Loop

Loop is the basic requirement of any programming languages. Loops are useful when you want to execute a series of commands until the certain condition is satisfied. In Bash, loops are useful for automating repetitive tasks. In this tutorial, we will see basics of for loop in Bash.

There are three basic loop constructs, for loop, while loop , and until loop. We will also learn how to use the break and continue statements.

The Standard Bash for Loop#

The for loop iterates over a list of items and performs the given set of commands.

Following is the form of Bash for loop:

for item in [LIST]
do
  [COMMANDS]
done

In the list items can be series of strings, an array, a range of numbers, output of a command, etc.

Loop over strings#

Let’s take an example, there is a list of strings and the variable element will be set to the current item:

for element in Chicago Boston Philadephia Portland
do
  echo "Element: $element"
done

The loop will produce the following output:

Element: Chicago
Element: Boston
Element: Philadephia
Element: Portland

Loop over a number range#

To specify a range of numbers or characters by defining start and end point of the range, use the sequence expression. Below is the form for sequence expression:

{START..END}

Below is the example loop which iterates through all numbers from 0 to 5:

for i in {0..5}
do
  echo "Number is : $i"
done
Number is : 0
Number is : 1
Number is : 2
Number is : 3
Number is : 4
Number is : 5

You also can specify the increment when using range. It’s form will be as following:

{START..END..INCREMENT}

Let’s see an example showing how to increment by 2:

for i in {0..10..2}
do
  echo "Number is : $i"
done
Number: 0
Number: 2
Number: 4
Number: 6
Number: 8
Number: 10

Loop over array elements#

If the data in array form you can use the for loop to iterate over array elements:

Here, an array named as CITIES and iterating over each element of the array.

CITIES=('Chicago' 'Boston' 'Philadephia' 'Portland')

for city in "${CITIES[@]}"; do
  echo "City: $city"
done
Book: Chicago
Book: Boston
Book: Philadephia
Book: Portland

break and continue Statements#

You can control the for loop execution by using the break and continue statements.

break Statement#

The break statement is usually used to terminate the loop when a certain condition is satisfied. It terminates the current loop and passes program control to the statement that follows the terminated statement.

Below is an example, in which we are using the if statement to terminate the execution of the loop when the current iterated item is equal to “Chicago”.

for element in Boston Philadephia Chicago Portland; do
  if [[ "$element" == 'Chicago' ]]; then
    break
  fi
  echo "Element: $element"
done

echo 'Done!'
Element: Boston
Element: Philadephia
Done!

continue Statement#

The continue statement is used to exit from the current iteration of a loop and go to the next iteration.

In below example given the range of numbers. When the current iterated item is matched with 3, the continue statement will execute to return to the beginning of the loop and to continue with the next iteration.

for i in {1..5}; do
  if [[ "$i" == '3' ]]; then
    continue
  fi
  echo "Number: $i"
done
Number is : 1
Number is : 2
Number is : 4
Number is : 5

Bash for Loop Example#

Below is the example to rename the all files of current directory which have space in name. We are replacing space to dash.

for file in *\ *; do
  mv "$file" "${file// /-}"
done

Let’s understand the code line by line:

  • First line creates a for loop and iterates through a list of all files.
  • The second line applies to each item of the list and moves the file to a new one replacing the space with an underscore (-). The part ${file// /-} is using the shell parameter expansion to replace a pattern within a parameter with a string.
  • At last, done indicates the end of the loop segment.

Conclusion#

In Bash, for loops are useful to execute a repetitive commands.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Gzip Command in Linux
Next Article   Du Command in Linux

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook