Looping Example using while statement in Linux

First, we create a text file using a text editor in Linux, in this case we are using `vim` Text Editor.

vim while.sh


#!/usr/bin/bash

a=7
while [ $a -gt 4 ];
do
echo $a
((a–))
done
echo “Out of the loop”

Explanation:

  • #!/usr/bin/bash: This line is called a shebang and indicates the path to the interpreter that should be used to execute the script. In this case, it specifies that the Bash shell should be used.
  • a=7: Initializes a variable named a with the value 7.
  • while [ $a -gt 4 ];: Starts a while loop that continues as long as the value of a is greater than 4.
  • do: Marks the beginning of the code block to be executed within the while loop.
  • echo $a: Prints the current value of the variable a to the console.
  • ((a--)): Decrements the value of a by 1. This is a shorthand way of writing a=$((a - 1)).
  • done: Marks the end of the code block for the while loop.
  • echo "Out of the loop": Prints “Out of the loop” to the console after the while loop has completed.

While Loop in Linux

In summary, this script initializes a variable `a` with the value 7, then enters a while loop that continues as long as `a` is greater than 4. Within the loop, it prints the current value of `a` and decrements it by 1 in each iteration. Once `a` becomes 4 or less, the loop exits, and the script prints “Out of the loop” to the console.

Bash Scripting – While Loop

A while loop is a statement that iterates over a block of code till the condition specified is evaluated to false. We can use this statement or loop in our program when do not know how many times the condition is going to evaluate to true before evaluating to false.  

Table of Content

  • The Syntax of a while loop in BASH Scripting
  • Looping Example using while statement in Linux
  • Reading a file with a while loop  
  • Infinite while loop
  • While loop to iterate for a fixed number of times
  • Read the command-line argument with getopts options
  •  C-style while loop
  • While loop to perform operations on a file
  • Writing to a file using a while loop
  • Break and continue Using while Loop
  • Frequeltly Asked Questions

Similar Reads

The Syntax of a while loop in BASH Scripting

while [ condition ];do # statements # commandsdone...

Looping Example using while statement in Linux

First, we create a text file using a text editor in Linux, in this case we are using `vim` Text Editor....

Reading a file with a while loop

We can read a file with a while loop in BASH.  By parsing certain parameters to the while loop condition, we can iterate over the file line by line or by other groups like characters or words....

Infinite while loop

To create an infinite loop using a while loop statement. We don’t need to put any condition in the while loop and hence the loop iterates infinitely. The below is the example of an infinite while loop:...

While loop to iterate for a fixed number of times

We can use a while loop to iterate over a fixed number of times, we can set the condition to be -le or less than equal to a number, and the loop will iterate till the iterator is less than or equal to the number provided in the condition. Also, we need to increment the iterator manually so to keep the loop iterator ticking, or else the loop will go on forever....

Read the command-line argument with getopts options

We can use get ops options to read the input from the command line and if there are multiple arguments, we can check them and parse them one by one using a while loop. getopts is a tool to get user input from the command line, We can have multiple options to parse from the command line, and using getopts and while loops, we can make a professional-looking user input program....

C-style while loop

We can use C-styled while loop in BASH, the variables need to be evaluated with BASH style but the overall syntax feels like C. We can use the operators like <,>,<= and so on in the condition of the while loop and hence it is called like the C-styled while loop....

While loop to perform operations on a file

We can perform operations on a file, like reading only a particular part of a file. If we have columns that are formatted in a particular fashion, we can use variables to assign them and print them one row data time....

Writing to a file using a while loop

We can write to a file by user input in a while loop. We can use the while loop to iterate till we manually exit out of the loop using CTRL + D by saving changes to the file or by CTRL + C for avoiding writing to the file. We use the read command to input the text from the command line and parse it to the file....

Break and continue Using while Loop

Break and continue are essential in a program that deals with loop statements as they control the flow of the loop statements without any explicit conditions....

Frequeltly Asked Questions

1. What is a `while` loop in Bash scripting?...

Conclusion

In this article we discussed the BASH scripting while loop which proves to be a versatile tool for executing a block of code repeatedly based on a specified condition. The basic syntax involves defining the condition within square brackets, allowing for flexible iterations. The article explores diverse applications of while loops, including reading from files, creating infinite loops, fixed iterations, parsing command-line arguments, and utilizing break and continue statements. From C-style syntax to interactive file writing, each example demonstrates the loop’s adaptability in addressing a range of scripting needs. This comprehensive overview underscores the while loop’s significance in BASH scripting, providing a robust mechanism for controlled repetition and efficient code execution....