Linux Bash While Loop Tutorial with Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-loop-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux bash provides a lot of mechanisms to make system administrators life easier. These features are similar to the programming language features like variables, decisions, loops etc. In this tutorial we will look loops which can be implemented with for and while we have already examined for loops in the following tutorial.

Bash For Loop Examples

What is Loop?

Loop is a mechanism where given items iterated one one by and given statement executed repeatedly. For example we want to print numbers to the console from 1 to 10 writing writing 10 times print statement is not an efficient way. So we can use a loop and iterate from 1 to 10 and print current item.

While Loop Syntax

While loop have simple syntax. We will define while  and the condition and then we put code we want to execute in every iteration between do  and done statements. CODE can be more than one line.

Count and Print From 0 To Specified Number

Now we will do a simple example. We want to count from 0 to the specified number which is 10 in this example. We will also use some comparison statements provided by bash. Comparison statements will compare wheter given conditions is met in each step.

Count and Print From 0 To Specified Number
Count and Print From 0 To Specified Number

Single Line Loop

We can create w loop in a single line by using bash ; separator. This will create multiple statements in single line. As we can see we created single line while loop but separated while , do and done .

LEARN MORE  Python For Loop Tutorial with Examples

Infinite Loops

There is a special loop example which is named infinite loop. As its name states this loops do not ends by itself. We can end this loop using external ways like cancel process by sending process signals. This loop can be useful if we need to check some values every time. If we set the condition always TRUE logic value this will be infinite loop.

Infinite Loops
Infinite Loops

Conditional Break During Loop with Break Statement

Some times we may need to break current loop if some condition is met. This will end loop even previously given condition is not met. We will use break to exit while loop. In this example if sum of given values are greater than 10 we will break the loop.

Conditional Break During Loop with Break Statement
Conditional Break During Loop with Break Statement

Continuation To Next Step with Continue Statement

In general we use some code for each iteration to process something. But in some cases we may need to skip given steps and done process. We can use continue  statement to skip next step. We should provide condition to skip given steps. In this example we will skip odd numbers.

LEARN MORE  Linux Bash For Loop Range Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-loop-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM