UNIX / Linux Shell Loop Types - Unix/Linux

What are UNIX / Linux Shell Loop Types?

In this section, we will consider shell loops in UNIX. A loop is a commanding programming tool that enables you to perform a set of commands constantly. In this section, we will observe the following types of loops available to shell programmers −

  • The while loop
  • The for loop
  • The until loop
  • The select loop


You will use different loops based on the situation. For example, the while loop executes the given commands until the given condition remains true; the until loop execute until a given condition becomes true.

Once you have good programming preparation you will gain the capability and thereby, start using suitable loop based on the situation. Here, while and for loops are accessible in most of the other programming languages like C, C++ and PERL, etc.

Nesting Loops

All the loops maintain nesting model which means you can put one loop inside another related one or different loops. This nesting can go up to infinite number of times based on your necessity.

Here is an instance of nesting while loop. The other loops can be nested based on the programming condition in a related way −

Nesting while Loops

It is feasible to use a while loop as part of the body of another while loop.

Syntax

Example

Here is a simple instance of loop nesting. Let's add another countdown loop inside the loop that you used to count to nine –


This will construct the following result. It is main to note how echo -works here. Here -n option lets echo avoid printing a new line nature.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics