X
Collapse
-
Bash Script Loops Examples
A loop in programming is a control structure that allows a specific code to be executed repeatedly until a condition is met. This process is repeated until no further action is required. Loop allows you to repeat the desired set of instructions numerous times to attain the desired outcome. These recursions can be useful for all tasks that require repetitive operations or when working with data collections.
In this article, we will explore the meaning, types, and examples of loops, further gaining insight into how to use them correctly, enabling you to develop powerful, concise, and adaptable code. As we mentioned earlier, three types of loops are available in bash scripting. Let’s divide this section into multiple parts to explain each loop briefly.
For Loop in Bash
For loop is used to execute specific commands or codes for particular times. Here is the basic syntax of the for loop:
for var in list
do
commands
done
For example, let’s use the for loop to print a message for 10 times:
div>
<div style="text-align: justify">!#/bin/bash
for i in {1..10}; do
echo "Hello, World!"
done
Once you execute the command, the system will print the message “Time for Linux” 10 times:
While Loop
You can use the while loop to execute the commands if a specific condition is true.
while [ condition ]
do
commands
done
For instance, let’s use the while loop, where the system will automatically exit whenever you press the S button on the keyboard:
#!/bin/bash
while :
do
read -n 1 k <&1
if [[ $k = S ]]
then
break
else
echo "You pressed '$k'"
fi
done
In the above script, K is the variable used for all the keys except the S. Moreover, the loop will break the condition after the entry of the S key:
Until Loop
The until loop is similar to the while loop, but it only continues if the specific condition is false and terminates when the condition is true.
until [ condition ]
do
commands
done
For example, you can use the until loop to reach a specific condition, so let’s create a script to print even numbers between o and 30.
#!/bin/bash
counter=0
until [ $counter -gt 30 ]; do
echo $counter
((counter+=2))
done
Once you run the script, the system will print all the even numbers until 30.
Wrapping Up
This was all about the simplest explanation behind bash script loops with examples. We have included multiple examples of various types of loops available in bash scripting. We recommend that you explore your creativity and skills to create amazing bash scripts around all the loops.
More...Tags: None
Posting comments is disabled.
Categories
Collapse
Article Tags
Collapse
There are no tags yet.
Latest Articles
Collapse
-
by KasimbaArch Linux hasn't had a license for any package sources (such as PKGBUILD files) in the past, which is potentially problematic. Providing a license will preempt that uncertainty.
In RFC 40 we agreed to change all package sources to be licensed under the very liberal 0BSD license. This change will not limit what you can do with package sources. Check out the RFC for more on the rationale and prior discussion.
Before we make this change, we will provide contributors with...-
Channel: Articles
11-19-2024, 09:21 AM -
-
by Kasimba
by George Whittaker
Introduction
In the world of cybersecurity and software development, binary analysis holds a unique place. It is the art of examining compiled programs to understand their functionality, identify vulnerabilities, or debug issues—without access to the original source code. For Linux, which dominates servers, embedded systems, and even personal computing, the skill of binary analysis is...-
Channel: Articles
11-18-2024, 07:10 PM -
-
by KasimbaDebian and Ubuntu are two popular Linux distributions. In this deep dive we will guide you on the key differences between them from perspective of both corporate enterprise and personal productivity or pleasure usage. After reading this blog post you should be in a better position to decide to select Ubuntu or Debian.
Stewardship, Licensing, Community and Cost
Where as Debian is 100% fully committed to free software as defined by the Debian Free Software Guidelines, Ubuntu is created...-
Channel: Articles
11-17-2024, 08:30 PM -
-
by Kasimba
by George Whittaker
Introduction
In the digital age, data loss is a critical concern, and effective backup and recovery systems are vital for any Debian system administrator or user. Debian, known for its stability and suitability in enterprise, server, and personal computing environments, offers a multitude of tools for creating robust backup and recovery solutions. This guide will explore these solutions,...-
Channel: Articles
11-13-2024, 05:30 PM -
-
by Kasimba
by George Whittaker
Introduction
Debian is one of the most trusted and stable Linux distributions, making it a top choice among developers and system administrators. Setting up a powerful development environment on Debian involves installing the right tools, compilers, libraries, and Integrated Development Environments (IDEs) that can support various programming languages and workflows. This guide provides...-
Channel: Articles
11-07-2024, 11:22 PM -
-
by Kasimba
by George Whittaker
In today’s digital world, data is more than just information; it’s a part of our lives. From photos and documents to sensitive personal information, our data represents our memories, work, and interests. While cloud storage services are widely available, they often come with privacy concerns, subscription fees, and limitations on customization. This is where building a personal cloud on Ubuntu comes...-
Channel: Articles
11-07-2024, 04:30 PM -