Create Your Bash While Loop
Here you will find out:
- how to create Bash While Loop
- Bash While Loop examples
- when DiskInternals can help you
Are you ready? Let's read!
Bash scripting is a bit technical, but over time, as you keep writing scripts, they become easier and easier. Nevertheless, you have a couple of questions to ask in regards to the Bash While loop, right?
Here’s everything you need to know, in detail.
Bash While Loop
There are basically three types of loops used creating bash scripts; the “While loop” is one of them and it is arguably the most common. Loops are used in programming to run specific commands several times until a condition is met. In Bash scripting, the Bash While loop is used to instruct a system to run a series of commands repeatedly following a defined condition.
Thus, in simple terms, a Bash While loop is used for automating commands in Bash scripting.
Bash While loop example
A While loop in a Bash script will trigger a series of commands to run repeatedly for as long as the condition result is “True.” Below is an example of Bash While loop syntax.
One-line While loop example:
The code above will instruct your system to run command1, command2, command3, and command4 continually until the condition turns to “True.”
Example 1: Infinite While loop in a shell script
An infinite While loop means your script will run the loop commands non-stop. Infinite While loops never stop running and this occurs when the condition always turns out to be “True.”
You can create an infinite While loop with the following command:
You can use the built-in ":"or "true" commands that always returns “True” or any other conditional statement.
Here’s the one-line expression:
Example 2: a loop with a fixed number of repeats
To repeat a loop for a fixed number of times, here’s how to do it:
Create a Bash file “script.sh” and write the following script inside:
The expression above will run the loop 3 times before printing the text content written inside the loop.
Example 3: a loop with a break statement
Everyone has a reason for adding a While loop to Bash shell scripts. Looping with a break statement means ending a loop early in a While loop.
To use a break statement in a While loop, use this command:
This example shows that a While loop is meant to repeat 8 times, but will exit after the 4th time to print the break statement.
How DiskInternals Linux Reader can help you access Linux partitions on Windows
If you’re running dual-boot, DiskInternals Linux Reader is a freeware tool that grants you access to view and access Ext4 partitions when you boot to Windows and need to check out some files saved in your Linux partition. DiskInternals Linux Reader features an intuitive, easy-to-understand interface and does not require any technical knowledge.
Also, this freeware works on virtual machines and it is apparently the best Ext4 explorer for Windows.