“`html
The bash shell offers various methods to iterate through data, making your tasks – particularly scripting challenges – more manageable.
You can traverse a large dataset and accomplish considerable work by executing a single script. Additionally, you have the ability to create and iterate through different sequences of values. Whether you’re looping through extensive numeric ranges, days of the week, usernames, words, file names, or any other items, bash provides options that simplify these processes.
Bash’s looping constructs include for loops, while loops, and until loops. This article showcases examples of each, ranging from basic to more complex.
One of the most straightforward loops is a for loop like the one demonstrated below. It iterates as many times as there are pieces of text provided as arguments (in this instance, 1, 2, 3, and 4). We can just as easily loop through the phrase “cats are very smart” as we do with the numbers 1, 2, 3, and 4.
“`
As previously mentioned, loops of this nature aren’t limited to just numeric variables. You can also iterate through various types of variables such as strings representing colors, states, planets, or even the days of the week. Here’s an illustrative example:
Once you execute this script and outline your plans, the “week” file will serve as a reminder of how you plan to allocate each day of your week.
You can implement a loop similar to those demonstrated below to traverse the letters of the alphabet or a series of numbers.
The for command illustrated below would showcase calendars for the last three months of the ongoing year.
The following script iterates through each month of the year.
While loops continue executing as long as the specified condition remains true. Here’s a sample:
The script above increases and displays the value of $n as long as it is 4 or less. You can create an infinite while loop (meaning it runs until the system fails, you log out, or someone terminates your script) using “while true” as demonstrated below. The sleep command is included to prevent it from executing excessively before you manage to stop it.
The script below remains active as long as the user it’s monitoring has not logged into the system. This can be beneficial when you’re waiting for a colleague to resolve an issue before proceeding with your work. It checks every minute to see if the user has logged in yet.
Until loops continue to execute as long as the specified condition remains false. For instance, we can implement a script utilizing an until command that operates until the count of the user being monitored exceeds 0.
Additionally, there’s nothing preventing you from nesting a looping command within another loop. In this scenario, we demonstrate a for loop nested inside a while loop.
This script iterates through the letters a, b, c, and d while counting numbers from 1 to 5.
The break and continue commands offer control over how bash loops function. The break command will terminate the loop and bypass any subsequent commands within it, whereas the continue command will skip the remaining commands and return to the loop’s start. In the example below, the break command is executed, terminating the loop when the user successfully guesses a randomly chosen 1- or 2-digit number.
The for, while, and until loops can significantly enhance your productivity with minimal effort, providing a variety of options for configuring the set of values they iterate over.