HomeLinuxFor, Whereas and Till Loops

For, Whereas and Till Loops


Loops are a robust function in any programming language. For those who have no idea already, the loops are a technique to repeat the code primarily based on sure standards.

For instance, think about that it’s a must to print the numbers from 1 to 10. You’ll be able to write the echo command ten instances however that is very primitive. You employ a loop and in 3-4 strains of code, it may be achieved.

That is the only of the examples I might consider. I’m going to share precise helpful examples whereas I talk about the bash loops with you.

There are three sorts of loops in Bash:

I will present all three sorts of looping within the tutorial. Let’s begin with the most typical one.

For loop in bash

This is the syntax for ‘for loop’ in bash:

for arg in LIST; do
 instructions
achieved

The LIST right here might be an array or an inventory of things. Brace expansions are additionally well-liked for looping.

Take the only situation I discussed at first. Let’s print numbers from 1 to 10 utilizing for loop:

#!/bin/bash

for num in {1..10}; do
	echo $num
achieved

For those who run it, it is best to see an output like this:

abhi[email protected]:~/bash_scripts$ ./for-loop.sh
1
2
3
4
5
6
7
8
9
10

You could possibly have additionally used for num in 1 2 3 4 5 6 7 8 9 10; do however utilizing the brace enlargement makes the code look shorter and smarter.

{..} is used for increasing on a sample. You employ {d..h} and it’s equal to d e f g h . Extra on brace enlargement might be discovered on this article.

Utilizing Brace Growth in Bash Shell

Brace enlargement within the bash shell is a lesser identified however an superior function. Study utilizing them like a Professional Linux consumer with sensible examples.

💡

In case you are acquainted with C programming, you could like utilizing the C-styled for loops in bash:

for ((i = 0 ; i < 10 ; i++)); do
echo $i
achieved

Let’s examine one other instance that shows all of the contents of an array in bash:

#!/bin/bash

distros=(Ubuntu Fedora Debian Alpine)

for i in "${distros[@]}"; do
	echo $i
achieved

For those who run the script, it would show all of the distros outlined within the array:

Ubuntu
Fedora
Debian
Alpine

Whereas loop in bash

The whereas loop assessments a situation after which retains on looping so long as the situation is true.

whereas [ condition ]; do
  instructions
achieved

For those who take the earlier instance, it may be rewritten utilizing the whereas loop like this:

#!/bin/bash

num=1
whereas [ $num -le 10 ]; do
	echo $num
	num=$(($num+1))
achieved

As you may see, you needed to outline the variable num to 1 first after which within the loop physique, you enhance the worth of num by 1. The whereas loop checks the situation and runs it so long as num is lower than or equal to 10.

Thus, operating the script now will present the precise consequence you noticed earlier with for loop.

1
2
3
4
5
6
7
8
9
10

Let’s examine one other instance. This is a bash script that takes a quantity as an argument and shows its desk.

#!/bin/bash

echo "Desk for $1 is:"
index=1
whereas [ $index -le 10 ]; do
	echo $(($1*$index))
	index=$(($index+1))
achieved

In case you are confused about using $1, it represents the primary argument handed to the script. Take a look at chapter 3 of this collection for extra particulars.

For those who run the script, it ought to present this output:

[email protected]:~/bash_scripts$ ./desk.sh 2
Desk for two is:
2
4
6
8
10
12
14
16
18
20

Till loop in bash

That is the lesser-used loop format. It behaves equally to the whereas loop. The distinction right here is that the loop runs till the situation it checks is fake. I will clarify it in a bit. Let’s examine its syntax first.

till [ condition ]; do
  instructions
achieved

Now, if I’ve to make use of the identical instance of printing numbers from 1 to 10 utilizing till loop, it will appear to be this:

#!/bin/bash

num=1
till [ $num -gt 10 ]; do
	echo $num
	num=$(($num+1))
achieved

The distinction is within the situation; the remaining stays the identical.

  • The whereas loop ran whereas the variable num was lower than or equal to 10.
  • The till loop runs till the variable num turns into better than 10.

Each are alternative ways of doing the identical factor. Whereas is extra well-liked as you may discover some time loop equal in most programming languages.

🏋️ Train time

That was enjoyable. Time to do some train now.

Train 1: Write a script that takes a quantity as an argument and prints its desk. Your script also needs to present a message if the script is run with out an argument.

Anticipated output:

$: ./desk.sh
You forgot to enter a quantity

$: ./desk.sh 3
3
6
9
12
15
18
21
24
27
30

Train 2: Write a script that lists all of the recordsdata within the listing /var

Trace: Use for loop with /var/* because the ‘record’.

You’ll be able to talk about your solutions on this devoted thread within the Neighborhood:

Apply Train in Bash Fundamentals Collection #8: For, Whereas and Till Loops

In case you are following the Bash Fundamentals collection on It’s FOSS, you may submit and talk about the solutions to the train on the finish of the chapter: Fellow skilled members are inspired to offer their suggestions to new members. Do observe that there might be a couple of reply to a given downside.

The bash fundamentals collection is coming to an finish. As the ultimate chapter within the collection, you may study to make use of features in bash scripting subsequent week. Keep tuned.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments