Linux

While, until & For Loop In Bash : How to use tutorial with examples

For Loop In Bash

Bash has been one of the most powerful scripting tools. In fact, the loops (specifically for loop in bash) are quite useful. As it was written for the GNU project. Now as it was the replacement for Bourne shell, so it is also known as GNU Bash.

Brain Fox was its writer and was first released in the year 1989. It was free software at its release and also to date.

The name Bash for this Unix Shell was acronym out of Bourne-again shell. It was also a pun on the name as it replaced the previous shell and had the notion of being born again.

The types of Loops available is Bash:

The various loops that Bash command has to offer are very useful. In this section, we are going to briefly explain all the loops that are used in Bash.

Loops allow us to repeat a set of commands to a particular number of times until some desired situation is reached. Bash scripting has three basic loops, which we will discuss in the following:

While Loop:

It is the easiest loop that Bash has to offer. As it is the exit controlled loop, it keeps on executing given lines of codes.

But, while the conditions are met or while the expression is true.

Syntax:

while[some test/expression]
do
<commands>
done

Until Loops:

These loops are very similar to while loops but with a very subtle difference. The tow loops: while loop and the until loop are so similar that their syntax is almost the same.

The only difference arises in the way they function. And this only difference is not that big.

The difference between the two can be explained as follows:

There is this thing called TEST COMMAND which has the reference of being called as the expression of the loop.

It is the expression according to which the loops operate.

In a while loop, until the expression is true the loop is iterated as soon as the expression becomes wrong the loop terminates.

In until loop until the expression is false, i.e. as long as the TEST COMMAND fails, the loop iterates. And it terminates when the TEST COMMAND is executed successfully.

Syntax:

until[some test/expression]
do
<commands>
done

For Loop:

Now time for the loop that we are all here for, the For loop!!

The for loop is completely different from the previous loops. The for loop basically iterates over a list , and then executes the given set of commands.

Basis Syntax of the for loop:

for var_name in <list>
do
<commands>
done

The utility of the For Loop:

For loop is the most basic of all the loops in every programming language and so is the case of Bash.

In the language of computers, the for-loop is a control-flow loop.

What this loop does is take a set of commands into consideration. Every time the loop is iterated these commands are executed.

It is best suited for scenarios in which you know about how many iterations are to be done for the desired result.

The for loop is used for iteration in the multi-dimensional arrays using nesting of the for a loop.

Various ways in which for loop is used in Bash:

Let us look at some examples for a better understanding of the for loop in Bash:

Example1:

for var_nam1 in Argon Neon Xenon Radon
do
echo “var_nam1 : $var_nam1”
done

Output:

var_nam1 : Argon
var_nam1 : Neon
var_nam1 : Xenon
var_nam1 : Radon

Example2:

In Bash for loops can also be used for printing sequence to a specified range. This can be done by defining a start and endpoint of the sequence range.

It has the following form: {START. .END}

[Note: This method of printing sequence range is only worked in Bash version 3 or later]

for j in {0. .4}
do
echo “Number : $j”
done

Output:

Number : 0
Number : 1
Number : 2
Number : 3
Number : 4

Also, from version 4 or later of Bash it is now possible to specify an increment while using ranges.

It has the following form: {START. .END. .INCREMENT}

for k in {0. .10. .3}
do
echo “Number : $k”
done

Output:

Number : 0
Number : 3
Number : 6
Number : 9

Example3:

A for loop inside the Bash command can also have three expressions contained inside it. Hence it’ll take the following form:

for (( exp_1; exp_2; exp_3 ))
do
command1
command2
command3
done

Basically it has the following meaning:

for (( INITIALIZATION; TEST; STEP ))
do
command1
command2
command3
done

Code:

for (( m=1 ; m <= 4 ; m++ ))
do
echo “Counter : $m”
done

Output:

Counter : 1
Counter : 2
Counter : 3
Counter : 4

Related posts:

Linux logging guide
Linux

Linux logging guide: Understanding syslog, rsyslog, syslog-ng, journalctl

I guess you have landed on the blog as you were facing some issue with linux logging, or maybe just
install mariaDB on CentOS 7
dB Linux

How to install mariaDB on CentOS 7 Within Minutes

Are you looking to install mariaDB on CentOS 7 operating system?, or are you facing any error while installing it.