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:

  • Over Strings

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

  • Over a Number Range

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

  • 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:

  • 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:

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:

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:

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:

Then run the following script:

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:

Then, run the below script:

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:

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!