for

shell programmingLinux/Unix
The for command is one of the most frequently used commands in Linux/Unix-like operating systems. for Shell loop construct for iterating through lists

Quick Reference

Command Name:

for

Category:

shell programming

Platform:

Linux/Unix

Basic Usage:

for [options] [arguments]

Common Use Cases

    Syntax

    for name [in words ...]; do commands; done
    for (( expr1; expr2; expr3 )); do commands; done

    Options

    Syntax Element Description
    name Variable name to hold each item in the iteration
    in words ... List of items to iterate through (if omitted, iterates through positional parameters)
    do commands Commands to execute for each iteration
    done Marks the end of the for loop
    (( expr1; expr2; expr3 )) C-style loop syntax where expr1 is initialization, expr2 is condition, expr3 is increment

    Examples

    How to Use These Examples

    The examples below show common ways to use the for command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    for i in 1 2 3 4 5; do echo "Number: $i" done
    Iterate through a list of numbers.
    for file in *.txt; do echo "Processing $file" done
    Process all text files in the current directory.
    for user in alice bob charlie; do echo "Hello, $user!" done
    Iterate through a list of names. # Advanced Examples Advanced for i in {1..10}; do echo "Iteration $i" done Use brace expansion to create a sequence. for (( i=0; i<5; i++ )); do echo "Counter: $i" done Use C-style for loop syntax. for i in $(seq 1 2 10); do echo "Odd number: $i" done Use command substitution with seq to generate a sequence. for file in $(find . -name "*.log"); do echo "Log file: $file" grep "ERROR" "$file" done Combine with find to process files matching a pattern. for server in server1 server2 server3; do ssh $server "uptime" done Execute commands on multiple remote servers.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The for command is a shell construct used for iteration, allowing a sequence of commands to be executed for each item in a list. Key features of the for loop: 1. List Iteration: The traditional for loop iterates through a list of items, executing specified commands for each item. 2. C-style Syntax: Bash also supports C-style for loops with initialization, condition, and increment expressions, which is useful for numeric iteration. 3. Variable Assignment: In each iteration, the loop variable is assigned the current item value and can be used within the loop body. 4. List Sources: The items to iterate through can come from direct listing, brace expansion, command substitution, filename expansion, or variable expansion. 5. Implicit Parameter Iteration: If the 'in words' clause is omitted, the for loop iterates through the positional parameters ($1, $2, etc.). 6. Flow Control: Within the loop, you can use break to exit the loop prematurely or continue to skip to the next iteration. 7. Nesting: for loops can be nested inside other loops or conditional statements for complex logic. Common use cases for the for loop include processing multiple files, performing actions on a series of items, generating sequences of commands, batch processing tasks, and automating repetitive operations in scripts.

    Related Commands

    These commands are frequently used alongside for or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the for command works in different scenarios.

    $ for
    View All Commands