Shell script "for" loop syntax

Learn shell script "for" loop syntax with practical examples, diagrams, and best practices. Covers unix, syntax, shell development techniques with visual explanations.

Mastering Shell Script 'for' Loops: A Comprehensive Guide

Hero image for Shell script "for" loop syntax

Explore the various syntaxes and applications of the 'for' loop in shell scripting, from basic iteration to advanced file processing.

The for loop is a fundamental construct in shell scripting, enabling you to automate repetitive tasks by iterating over a list of items, a range of numbers, or the output of a command. Understanding its different syntaxes is crucial for writing efficient and robust shell scripts. This article will guide you through the common forms of the for loop, providing practical examples and best practices.

Basic 'for' Loop Syntax: Iterating Over a List

The most common form of the for loop iterates over a list of words or items. Each item in the list is assigned to a variable, and the commands within the loop's body are executed for that item. The list can be explicitly defined, generated by command substitution, or expanded from wildcards.

for item in item1 item2 item3;
do
  echo "Processing: $item"
done

# Iterating over command output
for file in $(ls *.txt);
do
  echo "Found text file: $file"
done

# Iterating over files matching a pattern
for f in *.log;
do
  echo "Log file: $f"
done

Examples of basic 'for' loop iteration over explicit lists, command output, and file patterns.

flowchart TD
    A[Start Loop] --> B{Get next item from list}
    B -- No more items --> E[End Loop]
    B -- Item available --> C[Assign item to variable]
    C --> D[Execute commands with item]
    D --> B

Flowchart illustrating the basic 'for' loop execution.

C-Style 'for' Loop: Numeric Iteration

For scenarios requiring numeric iteration, similar to C or Java, shell scripting provides a C-style for loop. This syntax is particularly useful when you need to iterate a specific number of times, incrementing or decrementing a counter variable. It uses double parentheses ((...)) for arithmetic evaluation.

for (( i=1; i<=5; i++ ));
do
  echo "Count: $i"
done

# Decrementing loop
for (( j=10; j>=0; j-=2 ));
do
  echo "Countdown: $j"
done

Examples of C-style 'for' loops for numeric iteration.

Looping with seq and Brace Expansion

While the C-style loop is powerful, other methods exist for numeric iteration or generating sequences. The seq command (if available) can generate a sequence of numbers, and brace expansion {start..end} provides a concise way to create lists of numbers or characters.

# Using 'seq' command
for num in $(seq 1 3);
do
  echo "Number from seq: $num"
done

# Using brace expansion for numbers
for k in {1..5};
do
  echo "Brace expansion number: $k"
done

# Using brace expansion for characters
for char in {a..c};
do
  echo "Brace expansion char: $char"
done

Examples using seq and brace expansion for generating iteration lists.