LinuxCommandLibrary

for

Iterate over a list of items

TLDR

Iterate through command line arguments

$ for [variable]; do [echo $variable]; done
copy

Execute the given commands for each of the specified items
$ for [variable] in [item1 item2 ...]; do [echo "Loop is executed"]; done
copy

Iterate over a given range of numbers
$ for [variable] in [{from..to..step]}; do [echo "Loop is executed"]; done
copy

Iterate over a given list of files
$ for [variable] in [path/to/file1 path/to/file2 ...]; do [echo "Loop is executed"]; done
copy

Iterate over a given list of directories
$ for [variable] in [path/to/directory1/ path/to/directory2/ ...]; do [echo "Loop is executed"]; done
copy

Perform a given command in every directory
$ for [variable] in */; do (cd "$[variable]" || continue; [echo "Loop is executed"]) done
copy

SYNOPSIS

Iterate over a list:
for VARIABLE in LIST; do
COMMANDS
done

C-style arithmetic for loop (Bash/Zsh):
for (( INIT; CONDITION; INCREMENT )); do
COMMANDS
done

PARAMETERS

VARIABLE
    A variable that takes on the value of each item in the LIST during each iteration of the loop.

LIST
    A space-separated list of words, which can be filenames, strings, command substitutions, or shell expansions. The loop iterates once for each word in this list.

INIT
    (C-style loop) An arithmetic expression executed once before the loop begins, typically for initializing a counter variable.

CONDITION
    (C-style loop) An arithmetic expression evaluated before each iteration. If it evaluates to non-zero (true), the loop continues; otherwise, it terminates.

INCREMENT
    (C-style loop) An arithmetic expression executed after each iteration of the loop, typically for updating the counter variable.

COMMANDS
    The block of one or more shell commands to be executed during each iteration of the loop.

DESCRIPTION

The for command in Linux shell scripting is a fundamental control flow construct used for iterative execution. It allows a set of commands to be executed repeatedly, either for each item in a given list of words or by iterating through a sequence of arithmetic operations similar to C-style for loops. The primary use case is processing files, directories, numbers, or any sequence of strings. It's an essential tool for automating repetitive tasks, processing data sets, and manipulating file systems efficiently within shell scripts.

CAVEATS

The for in list loop performs word splitting and pathname expansion on the LIST before iteration. This can lead to unexpected behavior if filenames contain spaces or special characters, unless proper quoting (e.g., `"$item"`) or adjustments to IFS are made. The C-style for ((...)) loop is a Bash/Zsh extension and not universally portable to all POSIX shells like standard Bourne shell.

USAGE EXAMPLES

Iterating over files:
for file in *.txt; do
echo "Processing $file"
cat "$file"
done

C-style loop for numbers:
for (( i=1; i<=5; i++ )); do
echo "Number: $i"
done

Looping through command output:
for user in $(cut -d: -f1 /etc/passwd); do
echo "User: $user"
done

HISTORY

The for loop is a foundational construct present in the original Bourne shell (sh) from the late 1970s, making it a cornerstone of Unix shell scripting. The basic for VARIABLE in LIST; do ...; done syntax has remained largely consistent across various shells. The C-style arithmetic for ((...)); do ...; done loop was introduced later with Bash (Bourne Again SHell) to provide a more familiar looping paradigm for programmers coming from C-like languages.

SEE ALSO

while(1), until(1), select(1), break(1), continue(1), seq(1)

Copied to clipboard