


Let us consider an example for while loop and discuss the flow of the while loop using the example as below:Įxplanation to the above code: In the above, while loop program, first we initialized the variable a with value 0 and we started while loop with condition a < 10 where -lt is less than so it checks the condition as it evaluates to true the body of the while loop will execute which echo $a means, it displays the value of a and next statement a = `expr $a + 1` which will increment the value of a by 1 and again condition will check and execute the body of the loop until the condition evaluates to false and program execution goes to the statement next to the end of the while loop. By default, the while condition evaluates to true always and commands in while loop will execute infinite times.

If you want to perform some operation or execute a function infinite times then we can define while loop as below:Įxplanation to the above syntax: In the above while loop syntax method, there is no condition to check and perform operations based on the result of the condition. Once all the lines are finished than while loop will terminate The above while loop will read line by line from the text file in the path specified and execute the commands on the current line until all lines in the text file are read and operations are performed on them. If we want to check conditions on multiple lines of a text file then we can use the following syntax.Įxplanation to the above syntax: In the above syntax, while, do, and done are built-in keywords whereas IFS is used to set field separator which is by default a white space and -r indicates read mode. In the above syntax example, until the condition evaluates to true all the commands command1, command2 between do and done keywords will be executed and while loop will terminate when the condition is not satisfied and proceeds to statement next to the done keyword. We’ve got some built-in keywords in shell scripting, and while, do, and done, they’re among those. Web development, programming languages, Software testing & others First Syntax MethodĮxplanation to the above syntax: In the syntax above, the condition is checked on a variable so that if the condition is satisfied the commands will be executed.

Start Your Free Software Development Course
