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