Chat Zalo Chat Messenger Phone Number Đăng nhập
Bash Scripting - For Loop - GeeksforGeeks

Bash Scripting – For Loop – GeeksforGeeks

Since BASH is a command-line language, we get a fairly feature-rich experience to leverage programming skills to perform tasks in the terminal. We can use loops and conditional statements in BASH scripts to perform some repetitive and complicated problems in a simple programmatic way. In this article, we will focus on the for loop in BASH scripts.

There are a couple of ways to use for loops depending on the use case and the problem you’re trying to automate.

Simple For loop Range-based for loop

  • Matrix iteration
  • for loops

  • C-Styled for loops
  • Infinite for loop Simple For loop

  • To
  • execute a

  • loop
  • for we can write the following syntax:#!/bin/usr/env bash for n in a

b c;

do echo $n done

for1.PNG

The above command will iterate over the specified elements after the keyword in one by one. Items can be numbers, strings, or other forms of data.

Range-based for

loop

We can use range-based for loops. In this type of loop, we can specify the number to start, stop, and increment in each iteration (optional) in the instruction. There are two ways to do this, i.e. mentioning the increment/decrementer value and incrementing by one by default. The syntax looks like this:

#!/bin/usr/env bash for n in {1..5}; do echo $n done

for1.PNG

In the code above, we used the “{}” to specify a range of numbers. Within the curly brakes, we specify the start point followed by two points and an end point. By default, it is incremented by one. Therefore, we print 5 numbers from 1 to 5 inclusive.

#!/bin/usr/env bash for n in {1..5..2}; do echo $n done

for1.PNG

Here we can see that the loop increased by 2 units as mentioned in the braces. Therefore, this makes working with numbers very easy and convenient. This can also be used with alphabetic characters.

NOTE: We cannot use variables inside the braces, so we will have to encode the values. To use the variables, we see the traditional C style for loops in the following sections.

Array iteration for loops

We can iterate over arrays conveniently in bash using for loops with a specific syntax. We can use the special variables in BASH, i.e. @ to access all the elements of the array. Let’s look at the code:

#!/bin/usr/env bash s=(“football” “cricket” “hockey”) for n in ${s[@]}; do echo $n done

for1.PNG

We can iterate over the elements of the array using the @ operator which gets all the elements of the array. Therefore, using the for loop, we iterate over them one by one. We use the variable ${variable_name[@]} in which, the braces here expand the value of the variable “s” here, which is an array of strings. Using the [@] operator we access all the elements and therefore iterate over them in the for loop. Here, the “n” is the iterator, therefore we can print the value or do the required processing on it.

C-Styled

for loops

As stated earlier, we need to use the variables inside the for loops to iterate over a range of elements. And therefore, C-style loops play a very important role. Let’s see how we use them.

#!/bin/usr/env bash n=7 for (( i=1 ; i<=$n ; i++ )); do echo $i done

for1.PNG

As we can

see, we can dynamically use the value of the end condition range. Remember that spaces between double braces can be intentional and are part of the syntax. C-style loops are loops that have 3 parts, the initialization iterator, the incrementor/decrementer, and the final condition.

In the syntax above, we have initialized the loop iterator/counter to 1 which can be anything depending on the choice. The second part is the final condition, here we have used the variable “n” that is initialized before the for loop and so we use the simple operator $ to get the value of the variable. Finally, we have the incrementor/decrementer that changes the iterator/counter to a value that can be anything, but in the example, we have used the unary operator (++) to increment the value by one that is equivalent to “i=i+1”. Therefore, we can use statements like i + = 2, i-, ++ i, and so on.

Infinity for loop

We don’t use this often, but sometimes it’s useful for certain things to work. The syntax is fairly easy and similar to the C style for loops.

#!/bin/usr/env bash n=4 for (( ; ; )); do if [ $n -eq 9 ];then break fi echo $n ((n=n+1)) done

for1.PNG

As we can see, the for loop has no conditions and this repeats forever, but we have a condition statement to verify that it does not continue forever. We use the break statement inside the if statement to exit the loop and stop iterating with the iterator. We have used the incrementor to increment the variable in the loop, otherwise the loop is infinite. Of course, we need some logic to get out of the loop and that’s why we need to use the conditional statement if.

Contact US