Skip to content

Latest commit

 

History

History
250 lines (209 loc) · 4.21 KB

File metadata and controls

250 lines (209 loc) · 4.21 KB

for Loop in Bash

The for loop is a powerful and commonly used construct in Bash scripting. It allows you to iterate over a list of items and execute commands for each item in the list.


Syntax of for Loop

for variable in list
do
    # Commands to execute for each item
done
  • variable: A placeholder that takes the value of each item in the list during each iteration.
  • list: A sequence of items (numbers, strings, etc.) to iterate through.
  • do ... done: The block of code that executes for each item in the list.

Examples of for Loop

1. Iterating Over a List

#!/bin/bash
for fruit in apple banana cherry
do
    echo "I like $fruit"
done

Output:

I like apple
I like banana
I like cherry

2. Iterating Over a Range of Numbers

You can use brace expansion {start..end} to specify a range.

#!/bin/bash
for number in {1..5}
do
    echo "Number: $number"
done

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

You can also define a step value:

for number in {1..10..2}
do
    echo "Odd number: $number"
done

Output:

Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

3. Iterating Over Command Output

You can loop through the output of a command using command substitution.

#!/bin/bash
for file in $(ls *.txt)
do
    echo "Processing $file"
done

Output (example):

Processing file1.txt
Processing file2.txt
Processing file3.txt

4. Using a C-Style for Loop

Bash also supports C-style for loops.

#!/bin/bash
for ((i=1; i<=5; i++))
do
    echo "Count: $i"
done

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

5. Iterating Over Files in a Directory

You can use a wildcard to iterate over files.

#!/bin/bash
for file in /path/to/directory/*
do
    echo "File: $file"
done

Output (example):

File: /path/to/directory/file1
File: /path/to/directory/file2
File: /path/to/directory/file3

Special Features

Accessing Loop Counter

In standard for loops iterating over a list, the counter is the variable used in the loop:

for item in {A..C}
do
    echo "Item: $item"
done

For a C-style loop:

for ((i=0; i<3; i++))
do
    echo "Counter: $i"
done

Using Break and Continue

  • break: Exit the loop entirely.
  • continue: Skip to the next iteration.

Example with break:

for number in {1..10}
do
    if [ $number -eq 5 ]; then
        echo "Breaking the loop at $number"
        break
    fi
    echo "Number: $number"
done

Example with continue:

for number in {1..5}
do
    if [ $number -eq 3 ]; then
        echo "Skipping $number"
        continue
    fi
    echo "Number: $number"
done

Common Use Cases

1. File Processing

for file in *.log
do
    echo "Compressing $file"
    gzip "$file"
done

2. Renaming Files

for file in *.txt
do
    mv "$file" "${file%.txt}.bak"
done

3. Checking System Resources

for service in nginx apache2 mysql
do
    systemctl is-active --quiet $service && echo "$service is running" || echo "$service is stopped"
done

Best Practices

  1. Quote Variables: Use quotes to handle spaces or special characters in filenames or strings.

    for file in *.txt
    do
        echo "Processing \"$file\""
    done
  2. Use globbing carefully: Globbing like * can match unintended files if not handled properly.

  3. Use Arrays for Complex Lists:

    fruits=("apple" "banana" "cherry")
    for fruit in "${fruits[@]}"
    do
        echo "I like $fruit"
    done
  4. Validate Input: When looping over files or command outputs, ensure the inputs are sanitized to avoid unexpected behavior.


Common Pitfalls

  • Looping over command output with spaces: Use a while loop with read for better handling of spaces.

    ls | while read -r file
    do
        echo "File: $file"
    done
  • Overwriting Variables: Be cautious if the loop variable conflicts with other variables in the script.