for
shell loop construct for iteration
TLDR
Iterate over list
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.
