For example, if you want to go through a list or array of ‘n’ items, you’d use a for Loop. Let’s take a simple example: To perform any actions or to iterate the items in the above table, we require a For Loop.

Bash For Loop

In a bash script, For Loop syntax is as follows: Bash For Loop is quite straightforward. The first line #!/bin/bash indicates that the code is a bash script. VAR stands for the temporary variable used For Looping. N indicates the maximum number of iterations. ‘do’ and ‘done’ start and stop the Loop, respectively. Actions are the commands that execute within the Loop. We can execute Bash For Loop with different variables, like list, strings, integers, and arrays. This article will show a few common examples of the Bash For Loop. You can directly execute these programs on the bash command line or save them in a file and execute the file using the Bash filename.sh command.

Reading Static List

Consider the following list – rainbowColorList = Violet, Indigo, Blue, Green, Yellow, Orange, Red We can print the above string list using the Bash For Loop as follows: The output will be the list of items in a new line.

Reading an Array

The syntax for declaring an array is different. Use parentheses for each element (String). ‘@’ is used to iterate through each element in the array. We can also use the For Loop to print the indexes and the array elements. Note that we use ‘!’ in the Loop to get the element index.

Iterating Range of Numbers

We can use Bash For Loop to iterate over a range of numbers. The ‘..’ indicates a range of numbers. We can also skip count numbers by specifying the range. In the below example, we are skip counting by 3. The program above starts with 30 as the first number and counts down to 0. The last parameter in the for loop ‘3’ specifies the skip count number.

Strings and Characters

We can do a lot of interesting string operations with Bash For Loop. For example, we can read each character of a string by using the ‘seq’ operator in a For Loop: Note that the ‘seq’ should start with 1 to get the first character first. We can also print the strings separated by space one by one:

Expressions

Similar to any other programming language like Java, we can place expressions inside a Bash For Loop.

Reading Command-Line Arguments

To read from command-line arguments, we use the ‘read’ command. In the below example, we will get a few numbers from the user and print the sum using Bash For Loop. We use the variable total to store the intermediate and final total or sum of the numbers. The output is:

Finding Odd-Even Numbers

To find odd and even numbers between 1 to 10 (or any number N), we should use the if condition along with Bash for Loop. To determine an even number, we will divide the number by 2 and if the remainder is 0, then categorize it as even else odd. Note that we are giving the range as 10 in the above example. We can change this number to get even and odd numbers between any range. You can also try to read the number from the user using the ‘read’ command we learned in the previous section.

Infinite Loop

Infinity or infinite Loop is a loop that doesn’t stop executing, and the program must be forced to stop using Ctrl+C. We can easily create an infinite loop using the ‘; ;’ operator inside the for Loop:

Break Statement

Break statements are used to break from the Loop when an ‘if’ condition is satisfied. In this example, we are trying to find the color green. The For-Loop loops through each color, and once the green color is found, the program comes out of the Loop because of the break statement.

Continue Statement

Continue is used to skip the current Loop and move to the next based on a certain condition. For example, if you don’t want to print the color ‘Green’ from our previous program, we can put continue, and all other colors except Green will be displayed.

Final Words

We have covered the most common usages of a Bash For Loop, using strings, integers, arrays, and list. If you are learning Linux but have a Windows 10 machine, you can use the WSL feature and install Linux on your Windows machine. You can then use the Linux terminal in a similar fashion to CMD. Next, you can check out how to run bash scripts using Python.

10 Bash For Loop Examples with Explanations - 2610 Bash For Loop Examples with Explanations - 2410 Bash For Loop Examples with Explanations - 9210 Bash For Loop Examples with Explanations - 6410 Bash For Loop Examples with Explanations - 8810 Bash For Loop Examples with Explanations - 75