Wednesday, January 22, 2025
HomeProgrammingHow to Write a 'for' Loop in Bash Scripts

How to Write a ‘for’ Loop in Bash Scripts

When working with Bash scripts, loops are a fundamental feature that allow you to automate repetitive tasks efficiently. One of the most commonly used loops is the for loop. Whether you’re iterating over a list of items, processing files, or running commands multiple times, the for loop is a powerful tool to have in your scripting.

In this, we’ll explore the syntax of a for loop in Bash and look at practical examples to help you understand how it works.

Basic Syntax of a ‘for’ Loop in Bash

The general structure of a for loop in Bash is as follows:

for variable in list

do

#Commands to execute

done

variable: A placeholder for each item in the list.

list: A series of values you want to iterate over.

do…done: Encloses the commands to execute for each item.

Example 1: Iterating Over a List of Values

See also  How to create temp table using Create statement in SQL

The simplest use of a for loop is to iterate over a predefined list.

#!/bin/bash

for color in red blue green yellow

do

echo “The color is $color”

done

Output:

The color is red

The color is blue

The color is green

The color is yellow

Here, the loop assigns each value in the list (red, blue, green, yellow) to the variable color and executes the echo command.

Example 2: Iterating Over Files in a Directory

You can use a for loop to process files in a directory:

#!/bin/bash

for file in *.txt

do echo “Processing $file”

done

Output (for files like file1.txt, file2.txt):

Processing file1.txt

Processing file2.txt

The wildcard *.txt matches all .txt files in the current directory, and the loop processes each file one by one.

Example 3: Using a Sequence of Numbers

See also  What is Deletion in Binary Search Tree (BST)

To iterate over a sequence of numbers, use the seq command or brace expansion:

#!/bin/bash

for i in {1..5}

do

echo “Number $i”

done

Output:

Number 1

Number 2

Number 3

Number 4

Number 5

Alternatively, you can use the seq command for more flexibility:

#!/bin/bash

for i in $(seq 1 2 10)

do

echo “Number $i”

done

This example increments by 2, producing 1, 3, 5, 7, 9.

Example 4: Using Command Substitution

A for loop can iterate over the output of a command:

#!/bin/bash

for user in $(cut -d: -f1 /etc/passwd)

do echo “User: $user”

done

This loop extracts and prints all usernames from the /etc/passwd file.

Tips for Using ‘for’ Loops in Bash

1. Use Quotation Marks: Always use quotes around variables to avoid issues with spaces in filenames or list items. For example:

See also  Compiling a C Program: Behind the Scenes

for item in “file with space.txt” “another file.txt”

do echo “$item”

done

2. Test Before Running: If your loop involves critical commands, test it with echo before executing.

for file in *.txt

do

echo “Would process $file”

done

3. Avoid Overwriting Variables: Ensure your variable names don’t conflict with existing ones in your script.

Conclusion

The for loop in Bash is a versatile and essential feature for automating tasks. By mastering its syntax and variations, you can significantly enhance your scripting efficiency. Try out these examples, and experiment with combining loops with other Bash commands to tackle more complex tasks.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x