LinuxTutorials

Bash For Loop on Linux

Bash For Loop on Linux

In this tutorial, we will show you how to use bash for loop on Linux systems. If you are a Linux user or system administrator, you are probably familiar with the importance of automating tasks to save time and increase productivity. Bash scripting is an essential tool for achieving this goal, and Bash For Loops is a fundamental component of Bash scripting. In this guide, we will explore what Bash For Loops are and how to use them effectively to automate repetitive tasks on Linux. We will cover the syntax and structure of Bash For Loops, as well as provide several practical examples of how to use them in real-world scenarios. So, whether you are a beginner or an experienced Bash user, this guide will provide you with everything you need to know to start using Bash For Loops like a pro.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based and RHEL-based.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Bash For Loop on Linux

You can apply for a loop on the bash script in various ways. Some useful BASH for loop examples is mentioned in this article.

  • Standard Bash For Loop

Here is an example of how the Bash For Loop takes the form:

for item in [LIST]

do

[COMMANDS]

done
  • Over Strings

Now let’s look at standard Bash for Loop over Strings:

for element in Gold Silver Diamond Platinum

do

echo "Element: $element"

Done
  • Over a Number Range

Let’s look at the following example where it will iterate through numbers starting:

from 2 to 6.

for i in {2..6}

do

echo "Number: $i"

done
  • Over Array Elements

While one can use the for loop for numbers and strings, programmers can even leverage the same to repeat over a range. For example:

PLAYERS=('meilana' 'maria' 'ulfa')

for player in "${PLAYERS[@]}"; do

echo "Player: $player"

done
  • Break and Continue Statements

The break and continue statements can be used to control the for-loop execution.

Break Statement

To use the break statement, users have to specify a particular condition when met will break the loop. Programmers can even use the break statement when they want to stop the for-loop before planning:

for element in Gold Silver Diamond Platinum; do

if [[ "$element" == 'Diamond' ]]; then

break

fi

echo "Element: $element"

done

echo 'All Done!'

Continue Statement

The continue statement exits the current iteration of a loop and passes program control to the next iteration of the loop. For example, let us use the continue statement to iterate through a range of numbers, and when it reaches a specific number, which in this case will be ‘6’, the continue statement will exit the iteration and go back to the beginning of the loop to begin the next iteration:

for i in {2..8}; do

if [[ "$i" == '6' ]]; then

continue

fi

echo "Number: $i"

done

Bash for Loop Examples

There are 2 ways to use the For Loop in bash, one is in the ‘c-style syntax,’ and the other is ‘for-in.’ The syntax for the c-style for loops is as follows:

for ((INITIALIZATION; TEST; STEP))

do

[COMMANDS]

done

Meanwhile, as aforementioned, the ‘for-in’ loop will take the following form:

for variable in list

do

Statements

Done

Loop to Read Input Variable

Here is an example of how you can use them for a loop. In the example, we will take the input value of a text that has multiple words that will be taken after executing the script. We will use the for loop to divide the predefined text based on white space. Then ensure that the script will print each word:

#!/bin/bash

echo "Enter a text of multiple words"

read text

i=1

for word in $text

do

echo "Word No-$i = $word"

((i=$i+1))

done

Then run the following script:

bash forloop1.sh

Loop With a Break Statement

Programmers can use the for loop statement to read and print the list of files and folders of the current directory. First, create a file name and execute the following script. Users must note that the ‘*’ is used to read files in the ‘for loop.’ The functioning of the loop is simple manner by reading each file or folder through the step of the directory and printing with the output in the terminal with the ‘tab’ space:

printf "Pinting the files and folders of the current Directory...\n\n"

for list in *

do

printf "$list\t"

done

printf "\n\nDone\n"

Then, run the below script:

bash forloop1.sh

Changing File Extension

Using the for loop, one can change the file extension of all files in the current directory. For instance, we will replace all .jpeg with .png files using the for loop:

for file in *.jpeg; do

mv -- "$file" "${file%.jpeg}.png"

done

Congratulations! You have successfully learned the Bash For Loop with examples. For additional help or useful information, we recommend you check the official Bash website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!
Back to top button