Contact Info

Atlas Cloud LLC 600 Cleveland Street Suite 348 Clearwater, FL 33755 USA

support@dedirock.com

Client Area
Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3

The bash shell offers a variety of methods to iterate through data, making tasks – particularly scripting challenges – more manageable.

By looping through a collection of data, you can accomplish a great deal by executing a single script, and you can create and iterate through various sequences of values. Whether you’re cycling through a large set of numeric values, days of the week, usernames, words, filenames, or anything else, bash provides an option that simplifies the process.

The looping methods available in bash consist of for loops, while loops, and until loops. This article contains examples of each of these looping constructs, from basic to more complex.

The most straightforward example of a loop is a for loop, as demonstrated below. It iterates as many times as there are text fragments specified as arguments (in this instance, 1, 2, 3, and 4). We could just as easily iterate through the phrase “cats are very smart” as we could loop through the numbers 1, 2, 3, and 4.

As mentioned earlier, loops aren’t limited to numeric variables. You can also iterate through various types of variables such as strings representing colors, states, planets, or the days of the week. Here’s an illustration:

Upon executing this script and entering your plans, the “week” file will serve as a reminder of how you plan to spend each day throughout the week.

You might use a loop similar to those demonstrated below to traverse the letters of the alphabet or a series of numbers.

The following for command will show calendars for the last three months of the current year.

The script provided below iterates through each month of the calendar year.

While loops continue to execute as long as the specified condition remains true. Here is an illustration:

The script mentioned above increases and displays the value of $n as long as it is equal to or less than 4. You can create a while loop that runs indefinitely (until the system crashes, you log out, or someone terminates your script) by using “while true” as demonstrated below. The sleep command ensures that it doesn’t execute excessively before you can interrupt it.

The script shown below will continue to run as long as the user it is waiting for has not logged into the system. This can be useful when you are waiting for a colleague to resolve an issue before proceeding with your task. It checks every minute to see if the user has logged in yet.

Until loops operate under the principle of executing repeatedly until a specified condition becomes true. To achieve a similar outcome as illustrated in the earlier script, you can implement one using an until command that continues until the count of the user being monitored exceeds 0.

Moreover, there are no restrictions on nesting looping commands within others. For instance, in this example, a for loop is incorporated within a while loop.

This script iterates through the letters a, b, c, and d while counting from 1 to 5.

The break and continue commands serve to modify the behavior of bash loops. The break command will terminate the loop, bypassing any subsequent commands within it, whereas the continue command will skip the remaining commands in the loop and revert to its beginning. In the following script, the break command is executed, causing the loop to terminate when the user makes a guess of a randomly chosen 1- or 2-digit number.

The for, while, and until loops are valuable tools that allow you to accomplish tasks efficiently. They provide various options for configuring the range of values for looping.


Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.

Share this Post
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x