EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Unix Tutorial For Loop in Unix
Secondary Sidebar
Unix Tutorial
  • Advanced
    • Array in Unix
    • Functions in Unix
    • Unix File Permissions
    • Unix File System
    • Unix Shell Commands
    • Crontab in Unix
    • egrep command in Unix
    • For Loop in Unix
    • VI Editor in Unix
    • What is Unix Shell
    • Gnome Development
    • UNIX interview questions
    • Cheat sheet for UNIX
    • Unix Sort by Column
    • Find Command in Unix
    • Gnome Version
  • Basic
    • What is Unix
    • Uses of Unix
    • Career In Unix
    • Install UNIX
    • Unix Architecture
    • Unix Commands
    • VI Command in Unix
    • AWK Command in Unix
    • Paste Command in Unix
    • SED Command in Unix
    • Sort Command in Unix
    • WHO Command in Unix
    • Unix Operators
    • UNIX Administrator

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

For Loop in Unix

By Priya PedamkarPriya Pedamkar

For Loop in Unix

Overview of For Loop in Unix

A loop is nothing but a sequence of instructions that is repeated continuously until a certain condition is reached. It operates on a list of the item & repeats the set of commands for each item in a list. Loop is a fundamental concept of any programming language. In Unix, four kinds of loop programming are used:

  • While Loop
  • For Loop
  • Until Loop
  • Select Loop

Different loops are used in different situations. The use of the appropriate loop based on the situation is crucial for a good programmer. Specifically talking about the for loop. This loop is executed for the present number of times. Loop operates on a given list of items and executes the set of commands on every item on the list.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,328 ratings)

Syntax:

for item in element1 element2 element3 . .. element
do
Set of commands
done

Here item is the variable name assigned to the elements one by one. Here the element1 element2 element3 . . . element is a sequence of characters separated by spaces.

How For Loop Works in Unix?

Loop is used when you want to run a set of commands multiple times. Each time the loop is executed it is called an iteration. When the loop executes the value is assigned to a variable named item. It is set to the next element from a list of elements as we iterate over the loop. Let us understand the working of the loop with the help of an illustration.

How For Loop Works in Unix

Examples of For Loop in Unix

Let us understand the usage of for loop with the help of some basic examples.

Example #1

Let us look at an example with a simple for loop statement.

Code:

for var in 1 2 3 4 5
do
echo "Element $var"
done

Output:

For Loop in Unix eg1

Explanation: In the script shown above the for loop will iterate over the list (1 2 3 4 Since the list contains five elements and hence the loop runs five times. Echo command prints the statements as shown in the output above. The $ tells the shell interpreter to treat the var as a variable name and substitute its value in its place, rather than treat it as text or external command.

The flow of the loop can be altered by using the below-mentioned commands.

  • Break: Breaks the flow of the loop and steps down the code to the end of the loop. The purpose is to use when you want to terminate the execution of the entire loop once a certain set of commands is executed or the conditions are satisfied up to the break statement.
  • Continue: Continue command is similar to the break statement, except the fact that it exits the current iteration of the loop rather than the entire loop. The purpose is to use when an error has occurred and we are willing to execute the code further for other iterations of the loop.

Example #2

Let us look at an example with a break statement.

Code:

for var in 1 2 3 4 5
do
if [ $var == 4 ]
then
break
fi
echo "Element not equal to 4. Iteration no $var"
done

Output:

For Loop in Unix eg2

Explanation: In the script shown above the for loop will iterate over the list (1 2 3 4 5) and once the condition specified in the if-clause is satisfied it will execute the break statement and the loop will be terminated. The output is shown above clearly justifies the same. When the value of var is equal to 4 the loop gets terminated and no further iterations are printed by the echo command contained in the fi clause

Example #3

Let us look at an example with a continue statement.

Code:

for var in 1 2 3 4 5
do
if [ $var == 4 ]
then
continue
fi
echo "Element not equal to 4. Iteration no $var"
done

Output:

continue statement output

Explanation: Here as well the for loop will iterate over the elements and check if the value of the element is equal to 4. If the condition is satisfied it will skip the commands below the continue statement and will proceed to the next item of the iteration. In the example above the condition will get satisfied at the fourth iteration and the continue statement will be executed which will skip the echo command and will directly move to the fifth iteration. The same is reflected in the output as well.

Example #4

Using a range of numbers as well as increments.

Code:

for var in {1..10..2}
do
echo "Number: $var"
done

Output:

range of numbers output

Explanation: Here the loop starts with 1 and gets incremented by 2 until it reached 10. The above loop will be iterated five times and the output will be generated as shown above.

Important Points to Remember:

  • Shell scripting is case sensitive and hence proper syntax is to be followed while writing the shell scripting.
  • Indentations within the loops are not necessary. But it makes are script legitimate.
  • The elements of a list must be separated by spaces. If comma (,) is used it will be treated as a separate element of the list.
  • Also, the string character must not be enclosed in quotes otherwise it will be treated as the part of the string variable.

Conclusion

The loops are one of the most crucial concepts of structured programming techniques. It helps us to improve the productivity and robustness of the process through automation. With the use of loops, we can avoid typing redundant commands and which on a larger node helps us reduce the efforts and also the typographical errors.

Recommended Articles

This is a guide to For Loop in Unix. Here we discuss the syntax and working of For Loop in Unix along with examples and code implementation. You may also have a look at the following articles to learn more –

  1. SED command in Unix
  2. Unix Shell Commands
  3. Install UNIX
  4. Crontab in Unix
Popular Course in this category
All in One Software Development Bundle (600+ Courses, 50+ projects)
  600+ Online Courses |  3000+ Hours |  Verifiable Certificates |  Lifetime Access
4.6
Price

View Course

Related Courses

Linux Training Program (16 Courses, 3+ Projects)4.9
Kali Linux Training (3 Courses, 3+ Projects)4.8
Red Hat Linux Training Program (4 Courses, 1+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more