LinuxCommandLibrary

for

shell loop construct for iteration

TLDR

Iterate over list

$ for item in [a b c]; do echo $item; done
copy
Loop over files
$ for file in *.txt; do cat "$file"; done
copy
C-style loop
$ for ((i=0; i<10; i++)); do echo $i; done
copy
Loop with range
$ for i in {1..10}; do echo $i; done
copy
Process command output
$ for line in $(cat [file.txt]); do echo "$line"; done
copy

SYNOPSIS

for name in words; do commands; done

DESCRIPTION

for is a shell keyword that iterates over a list of items, executing commands for each. It's one of the fundamental loop constructs in shell scripting.
The loop variable takes each value from the word list in turn. Brace expansion, command substitution, and globbing can generate the list. C-style syntax provides numeric iteration.
for loops are essential for batch processing files, generating sequences, and iterating over arrays.

PARAMETERS

NAME

Variable name for each iteration.
WORDS
List of items to iterate over.
COMMANDS
Commands to execute each iteration.
in
Introduces the list of items.
do
Begins the command block.
done
Ends the for loop.

CAVEATS

Word splitting can cause issues with spaces. Quote variables in commands. Large lists may be slow.

HISTORY

for loops are part of the Bourne shell from the 1970s and are specified by POSIX. The C-style syntax was added by bash and ksh for compatibility with C programmers.

SEE ALSO

while(1), until(1), bash(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community