Bash is a popular shell used on Linux and macOS systems. It is a powerful tool for automating repetitive tasks and can help streamline system administration and development tasks. One of the most commonly used features of Bash is the for loop.

Advertisement

In this tutorial, we will cover the basics of for loops as well as advanced for loops examples in Bash and show you how to use them to automate your work.

What is a for loop in Bash?

A for loop is a control structure in Bash that allows you to execute a set of commands repeatedly. It is particularly useful when you need to perform a task a certain number of times or when you need to process a list of items.

A for loop has three main components:

  • The `for` keyword
  • A variable that will hold each item in the list
  • The list of items to iterate over

Here’s the basic syntax of a for loop in Bash:

The for loop starts with the for keyword, followed by the variable name that will hold each item in the list, the in keyword, and the list of items to iterate over. The loop body is enclosed in the do and done keywords.

Using for loops in Bash

Let’s look at some examples to see how for loops work in Bash.

Example 1: Printing numbers from 1 to 10

Here is a basic example, that will print numbers.

In this example, we’re using a for loop to print numbers from 1 to 10. We’re using a range of numbers enclosed in curly braces as the list of items to iterate over. The loop body consists of a single command that prints the value of the loop variable i.

Example 2: Looping through a list of items

You can define a list of items to iterate through by the for loop.

In this example, we’re using a for loop to iterate through a list of items – apple, banana, and orange. The loop body consists of a single command that prints a message using the value of the loop variable fruit.

Example 3: Looping through files in a directory

for loops can be used to iterate over files and directories in Bash. Here’s an example that shows how to use a for loop to process all the files in a directory:

In this example, we’re using a for loop to iterate through all the files in a directory. The loop variable file is set to the name of each file in turn. The loop body consists of a single command that prints a message indicating that the file is being processed.

Example 4: Nested for loops

Nested for loops can be used to iterate over multiple lists of items. The outer loop runs through one list of items, while the inner loop runs through another list. Here’s the basic syntax of a nested for loop in Bash:

Let’s look at an example that uses nested for loops to print all possible combinations of two letters:

In this example, the outer loop runs through the letters a to z, while the inner loop also runs through the letters a to z. The loop body consists of a single command that concatenates the current values of the loop variables i and j.

Example 5: Using arrays with for loops

Bash arrays can be used with for loops to iterate over a list of items. Here’s an example that shows how to use an array with a for loop:

In this example, we’re using an array named fruits that contains three items – apple, banana, and orange. The for loop iterates over the array and the loop variable fruit is set to the value of each item in the array in turn.

Conclusion

In this tutorial, we covered the basics and advanced for loops examples in Bash. We showed you how to use for loops to automate repetitive tasks, iterate over lists of items, and process files in a directory. for loops are a powerful feature of Bash that can help you save time and streamline your workflow. With a little practice, you can start using for loops to automate your work and make your life easier.

Share.
Leave A Reply

Exit mobile version