Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash For Loop - DesignLinux

Oct 16 2020
designlinux 0 Comments
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.

Related

Tags: bash, loop, terminal

How to Backup and Restore a PostgreSQL Database

Prev Post

How to Install Odoo 14 on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 605
  • 1,055,377

DesignLinux.com © All rights reserved

Go to mobile version