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. For those of you who didn’t know, Loops are one of the fundamental concepts of programming languages. Loops are handy when you want to run a series of commands over and over again until a certain condition is reached. Like any other programming language, bash shell scripting also supports ‘for loops’ to perform repetitive tasks. It helps us to iterate a particular set of statements over a series of words in a string, or elements in an array.

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.

Bash For Loop on Linux

You can apply for a loop on the bash script in various ways. Some useful BASH for loops 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 number 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 planned:

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 number, 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 the simple manner by reading each file or folder through the step of the directory and prints 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 file 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 to 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!