• Javascript
  • Python
  • Go

Writing a 'for' loop in Bash

Bash is a powerful scripting language used for automating tasks and managing systems. It is commonly used in Linux and Unix environments and...

Bash is a powerful scripting language used for automating tasks and managing systems. It is commonly used in Linux and Unix environments and has a wide range of functionalities. One of the most useful features of Bash is the 'for' loop, which allows you to execute a set of commands repeatedly.

In this article, we will explore how to write a 'for' loop in Bash and understand its syntax and usage.

To begin with, let's first understand what a 'for' loop is. A 'for' loop is a control structure that executes a set of commands repeatedly until a specific condition is met. It is useful when you need to perform a task multiple times with slight variations in the input values.

Now, let's dive into the syntax of a 'for' loop in Bash. The basic structure of a 'for' loop in Bash is as follows:

for variable in list

do

commands to be executed

done

Let's break this down further. The first line starts with the keyword 'for' followed by a space and then a variable name. This variable will be used to store the values from the list that we will iterate over. Next, we have the keyword 'in' followed by a list of values. These values can be specified explicitly or can be generated using other commands.

After the list, we have the keyword 'do' followed by a new line. This is where we specify the commands that we want to execute in each iteration. These commands can be any valid Bash commands, and they will be executed for each value in the list. Finally, we end the loop with the keyword 'done' on a new line.

Now, let's see a practical example of a 'for' loop in action. Suppose we want to print the numbers from 1 to 10 on the terminal. We can achieve this using the following 'for' loop:

for num in 1 2 3 4 5 6 7 8 9 10

do

echo $num

done

In this example, we have used the variable 'num' to store the numbers from 1 to 10, and the 'echo' command is used to print the value of the variable on the terminal. The output of this loop will be:

1

2

3

4

5

6

7

8

9

10

As you can see, the 'for' loop has iterated over the list of numbers and executed the 'echo' command for each value.

But what if we want to print only even numbers from 1 to 10? In that case, we can use the 'seq' command to generate a list of even numbers and use it in our 'for' loop, like this:

for num in $(seq 2 2 10)

do

echo $num

done

Here, we have used the 'seq' command with the following syntax: 'seq starting_number increment_number ending_number'. This will generate a list of even numbers starting from 2 and ending at 10 with an increment of 2. The output of this loop will be:

2

4

6

8

10

As you can see, we have achieved our desired result by using a combination of the 'for' loop and the 'seq' command.

Another useful feature of the 'for' loop in Bash is the ability to iterate over files in a directory. Let's say we have a directory containing some text files, and we want to print the contents of each file. We can do this using the following 'for' loop:

for file in *

do

cat $file

done

Here, we have used the wildcard '*' to specify that we want to iterate over all files in the current directory. The 'cat' command is used to print the contents of each file. This loop will execute the 'cat' command for each file, and the output will be the contents of all the files in the directory.

In conclusion, the 'for' loop is a powerful tool in Bash that allows you to automate repetitive tasks and perform different operations on a set of values. It is a widely used construct and is an essential part of any Bash programmer's arsenal. So, the next time you have to perform a task multiple times, remember the 'for' loop and make your life easier.

Related Articles

Align Text to the Right - Bash

In the world of coding, there are many different languages and tools that developers use to create and manipulate code. One of these tools i...