Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Linux- Shell Loops

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Have you used Linux as an operating system? Have you done some programming in the shell in Linux?

This article focuses on one of the critical shell programming concepts, Loops. We will mainly concentrate on different Loops in Shell. We will also learn the implementation using an example. Let’s see all these things in more detail.

Looping Statements in Shell Scripting

A loop is a vital and powerful programming tool. It allows you to execute a set of commands in repetition. We can run a command or a series of commands multiple times. 

Following are the types of loop statements that are available in Shell Scripting:

  1. While loop,
     
  2. For loop, and
     
  3. Until loop.
     

You can also change the flow of the loops using two keywords. These keywords are:

  1. Break: You can use it to break any loop.
     
  2. Continue: You can use it to break the current iteration in any loop. Then it moves to the next iteration. 
     
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

While Statement

In the "While" statement, the first step is condition evaluation. After that, It will initiate the command execution. It will run the commands till the conditions are true. In case of false, it breaks the loop or repetition.

Example

Let's check out an example of the "While" loop. In this example, we will try to print the addition of the first 'n' natural numbers.

 

Program:

echo "Enter a number: "
read n

i=1
sum=0
# Calculation of the sum of the first 'n' natural numbers with the help of the 'while' loop.
while [ $i -le $n ]
do           
  sum=$((sum + i))
  i=$((i + 1))
done

echo "The sum of First "$n "natural numbers is "$sum"."

 

Output:

output1


Program 2: We are going to create infinite loop using the “while” loop.

echo ”The while loop is starting”
while true
do
echo “I am Universe…”
done

 

Output:

output

For Statement

In the "For" statement, the process is almost the same as the "while" loop. When you are working on a list of items, you can use the "For" loop. For every item in the loop, it will repeat the execution of the command. 

Example

Let's check out an example of the "For" loop. In this example, we will try to print the addition of the first 'n' natural numbers. 

 

Program 1:

echo "Enter a number: "
read n

sum=0

# Calculation of the addition of the first 'n' natural numbers with the help of the 'for' loop.
for((i=1;i<=n;i++))
do
    sum=$((sum + i)) 
done

echo "The sum of First "$n "natural numbers is "$sum"."

 

Output:

output

 

Program 2: We are going to use “for” loop to iterate through a list. We will also use “break” keyword.

echo "Enter a number: "
read n

# Finding the number ‘n’ with the help of the 'for' loop.
For i in 10 9 8 7 6 5 4 3 2 1
do
if[ $i -eq $n ]; then
echo “$n found!”
break
fi
echo “$i”
done

 

Output:

output

Until Statement

In the "Until" statement, the process is also almost the same as the other loops. It will work until the condition specified is true. In case the condition has become false, the loop will break.

Example

Let's check out an example of the "For" loop. In this example, we will try to print the first 'n' natural numbers.

 

Program 1:

echo "Enter a number: "
read n

i=1
sum=0

# Calculation of the addition of the first 'n' natural numbers with the help of the 'until' loop.
until [ $i -gt $n ]
do           
  sum=$((sum + i))
  i=$((i + 1))
done

echo "The sum of First "$n "natural numbers is "$sum"."

 

Output:

output


Check out this problem - Subarray Sum Divisible By K

Frequently Asked Questions

What is the shell?

Shell is a command language interpreter. It executes commands read from input devices like keyboards or files.

Is Linux a C or C++?

Linux is written mainly in C programming language, with some parts in assembly-level language.

Is shell easy to learn?

Shells allow users to communicate efficiently and directly with their operating systems. Shell scripting is not a single language, but because it uses some natural language commands, it's easy to learn, even without a programming background.

Do coders use Linux?

Many developers and programmers tend to choose Linux Operating Systems over other Operating Systems. This is because it enables them to work more effectively and quickly. It will enable them to customize as per their needs and be innovative. 

What is the difference between Bash and shell?

Bash stands for “Bourne Again SHell.” It is an improvement of the sh, which is the original Bourne shell. Shell scripting is in any shell, whereas Bash scripting is specifically for Bash. sh is a shell command-line interpreter of Unix/Unix-like operating systems.

Check this out: Touch command in linux

Conclusion

In this article, we have studied the loops in Shell Programming in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding the Linux-Shell and if you would like to learn more, check out our articles on Linux Kernel and Linux Directories.

Recommended Read, Characteristics of OOPS

Learn more, wget command in linux

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Topics covered
1.
Introduction
2.
Looping Statements in Shell Scripting
3.
While Statement
3.1.
Example
4.
For Statement
4.1.
Example
5.
Until Statement
5.1.
Example
6.
Frequently Asked Questions
6.1.
What is the shell?
6.2.
Is Linux a C or C++?
6.3.
Is shell easy to learn?
6.4.
Do coders use Linux?
6.5.
What is the difference between Bash and shell?
7.
Conclusion