Souy Soeng

No title

For Loop in Bash

The for loop in Bash is used to iterate through a list of items, files, numbers, or any set of values. It executes a set of commands for each item in the list, making it an essential tool for automation and scripting.

Basic Syntax

for variable in list do # Commands to execute done
  • variable: A placeholder for each item in the list.
  • list: A space-separated list of items.
  • do and done: Enclose the block of commands.

Examples of For Loops

1. Loop Through a List of Words

Script:

#!/bin/bash for word in apple banana cherry do echo "Fruit: $word" done

Output:

Fruit: apple Fruit: banana Fruit: cherry

2. Loop Through Numbers

Script:

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

Output:

Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
  • {1..5}: Generates numbers from 1 to 5.

3. Loop Through a Range with Step

Script:

#!/bin/bash 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
  • {1..10..2}: Generates numbers from 1 to 10, incrementing by 2.

4. Loop Through Files in a Directory

Script:

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

Output:

File: /path/to/directory/file1.txt File: /path/to/directory/file2.txt File: /path/to/directory/file3.txt
  • /path/to/directory/*: Matches all files in the directory.

5. Loop Using Command Substitution

Script:

#!/bin/bash for user in $(who | awk '{print $1}') do echo "Logged-in user: $user" done

Output:

Logged-in user: alice Logged-in user: bob
  • $(command): Executes a command and uses its output as the list.

6. C-Style For Loop

Bash also supports a C-like syntax for for loops.

Script:

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

Output:

Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
  • ((i=1; i<=5; i++)): Initializes, checks, and increments the loop variable.

7. Nested For Loop

You can nest for loops to iterate over multiple lists.

Script:

#!/bin/bash for i in 1 2 3 do for j in A B C do echo "$i$j" done done

Output:

1A 1B 1C 2A 2B 2C 3A 3B 3C

Best Practices

  1. Quote Variables: Use double quotes around variables to handle spaces in values.

    for item in "apple pie" "banana split" do echo "$item" done
  2. Avoid Hardcoding Paths: Use variables for paths or filenames to make scripts reusable.

    directory="/path/to/files" for file in "$directory"/* do echo "$file" done
  3. Debugging Loops: Add set -x at the top of your script to trace execution.

Common Use Cases

  1. Batch Renaming Files

    for file in *.txt do mv "$file" "${file%.txt}.bak" done
  2. Performing Tasks on Multiple Servers

    for server in server1 server2 server3 do ssh "$server" "uptime" done
  3. Processing Log Files

    for log in /var/log/*.log do grep "ERROR" "$log" done

Conclusion

The for loop in Bash is a versatile tool for iterating over lists, processing files, and automating repetitive tasks. It’s a fundamental concept that every Bash user should master.

Let me know if you'd like examples of advanced use cases or troubleshooting tips!

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close