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