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:

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:

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:

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:

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:

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!