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.
Example:
Explicit example:
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.
Related articles
- How to Access Linux Ext2 or Ext3 on Windows
- Mount Linux Drive on Windows for Free
- Guide in Pictures
- A Bash Dirname Command
- Learn about Korn shell scripting
- Bash: how to split strings in Linux
- Here is everything you need to know about Bash for Windows
- How to Use Shell Script Sleep Command
- Linux: Sudo in Bash Scripts
- Use a Shell Script to Send an Email
- Learn about useful bash scripts
- Learn about systemd startup script
- Using /usr/bin/env command
- The disk you inserted was not readable by this computer error
- Shell script usage
- About a bash date command
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Bash Script: All You Need to Know
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A crontab service shell script
- A Bash Multiline Command
- Bash and sh: is there any difference
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Install Oracle Database
- AWK in a Bash Script
- How to Use Linux Wait Command
