Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Until Loop
2.1.
Syntax and Example for Until Loop
3.
Select Loop
3.1.
Syntax and Example for Select Loop
4.
For Loop
4.1.
Syntax and Example for For Loop
5.
Frequently Asked Questions
5.1.
What differentiates a statement from a loop?
5.2.
Can the Do Until loop be empty?
5.3.
What does the dollar sign ($) mean?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Shell Scripting - Until, Select and For Loop

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

A loop is a set of repeated instructions until a specific condition is satisfied in computer programming. Let’s take an example. We want to perform an action, such as modifying a data set. To do so, we will write a function for this operation and put it inside a recurrence relation (here, a loop). This function will get repeated until the necessary condition of the loop is satisfied.

shell scripting until select and for loop

This article will discuss the until, select, and for loops in shell scripting. We will see syntax and examples for each type of loop. 

So without further ado, let’s get started!

Until Loop

The while loop is ideal when you need to run a series of commands while a condition is true. Sometimes you must run a series of instructions until a specific condition is met. In these cases, we use another loop called the “until” loop. 

Until loop is quite similar to a while loop, the main difference is that the while loops are intended to continue running while a condition is met and end when that condition returns false. Contrarily, until loops are intended to continue running even if the condition returns false and only end when the condition does.

Syntax and Example for Until Loop

Syntax for Until Loop

The syntax for the until loop is as follows:

until command
do
   Statement(s) to be executed until the command is true
done

 

The Shell command is assessed here. The specified statement or statements are carried out if the outcome is false. The programme skips the statement and jumps to the next line following the done statement if the command returns true.

 

Example for Until Loop

#!/bin/sh
n=1
until [ ! $n -ge 10 ]
do
   echo $n
   n=`expr $n + 1`
done

 

Output:

output for until loop

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

Select Loop

The select construct allows us to generate some easy menus. So whenever we are required to generate some easy menus, then we generally use the select loop.

Syntax and Example for Select Loop

Syntax for Select Loop

The syntax for the select loop is as follows:

select var in i1 i2 ... iN
do
   Statement(s) to be executed for every word.
done

Here, i1 to iN are string sequences of characters separated by spaces. The var is the name of a variable (words). The value of the variable var is changed to the following word in the list of words, word1 to wordN, each time the for loop runs.

 

Example for Select Loop

#!/bin/sh
select name in mark john tom ben
do
   echo "$name selecte"
done

 

Output:

output for select loop

For Loop

In our discussion of loops in shell scripting, we will now discuss the for loops. The for loop controls the lists of items in the loop. Just like in other languages, the for loops repeat a set of conditions for every item in the list.

Syntax and Example for For Loop

Syntax of the for loop

The syntax of the for loop is as follows:

for var in i1 i2 ... iN
do
   Statement(s) to be executed for every word.
done

 

Here, i1 to iN are string sequences of characters separated by spaces. Var is the name of a variable (words). The value of the variable var is changed to the following values in the list of values, i1 to iN, each time the for loop runs.

 

Example of the for loop

#!/bin/sh
for n in 1 2 3 4 5
do
   echo $n
done

 

Output:

output for loop

 

Syntax of the for loop to execute a command

The syntax of the for loop to execute some commands is as follows:

for command in (type the commands here)
do
   Statement(s) to be executed for every word.
   $command
done

 

The syntax to execute the command in for loop is very similar to what we had seen above. Here we type the command in the form of a list of commands. Here is a list of some basic commands that you should know.

unix command

Example of the for loop to execute a command

for command in ls pwd
do 
   echo"-----------------$command-----------------"
   $command
done

 

Output:

output for loop

Frequently Asked Questions

What differentiates a statement from a loop?

Similar to a while loop, a while statement will keep running until the condition becomes false. The loop's execution sequence is the only distinction.

Can the Do Until loop be empty?

A statement that can either be TRUE or FALSE is required for a do-until loop. In this instance, we're using the IsEmpty function, which tests a particular cell to see if it's empty and returns FALSE if it does. It returns TRUE if there is no value present.

What does the dollar sign ($) mean?

The preceding command's exit status is represented by the $ variable. Upon successful completion, every command returns a numerical number known as the exit status. The majority of commands typically return an exit status of 0 when successful and 1 when failed.

Conclusion

This article briefly discussed until, for and select loop in shell scripting. We discussed syntax and examples of each type of loop. We hope this blog has helped you enhance your knowledge about the until, select and for loop in shell scripting. If you like to learn more, you can check out our articles: 

🌐 Introduction to Linux Shell & Scripting Shell

🌐 Linux Screen Command

🌐 Shell Scripting Interview Questions

🌐 Linux Operating System

🌐 Shell Scripting While Loop

ping command in linux

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptAWS and many more! If you wish to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

If you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Shell Scripting - While Loop
Next article
Shell Scripting - Pass Arguments to a Bash-Script
Live masterclass