Windows
Linux
Terminal & Shell Scripting
Terminal
Windows for equivalent in Linux
Repeats a block of commands for each item in a list, range, or set of files.
Windows
→
for
Linux equivalents
for ... in ...; do ... done
for ... in ...; do ... done
for ... in ...; do ... done
Overview
What this command does
Repeats a block of commands for each item in a list, range, or set of files.
Migration tip: Bash
for loops are much more flexible than Windows batch. Loop over files with globs, sequences with {1..10}, or command output with $(command).Practical tasks
Common use cases
Loop over a list or range
Repeats a block of commands for each item in a list, range, or set of files.
for i in {1..10}
Loop from 1 to 10
for f in *.txt
Loop over all .txt files
for i in $(cat list.txt)
Loop over lines in a file
Ready to run
Copyable Linux commands
Review paths, device names, package names and permissions before running any command.
for file in *.log; do
echo "Processing: $file"
gzip "$file"
done
for i in {1..5}; do
echo "Step $i"
Walkthrough
Examples with explanations
# Windows: for %i in (*.txt) do echo %i
for file in *.log; do
echo "Processing: $file"
gzip "$file"
done
# Numeric range:
for i in {1..5}; do
echo "Step $i"
done
Reference
Common options and variations
| Command or option | Use |
|---|---|
for i in {1..10} | Loop from 1 to 10 |
for f in *.txt | Loop over all .txt files |
for i in $(cat list.txt) | Loop over lines in a file |
for ((i=0; i<10; i++)) | C-style numeric loop |
Distro differences
Debian/Ubuntu vs Fedora vs Arch
This command is the same on Debian/Ubuntu, Fedora and Arch Linux when the relevant tool is installed.
| Distribution | Package manager / base | Equivalent command | Difference to notice |
|---|---|---|---|
| Debian/Ubuntu | same command | for ... in ...; do ... done |
Same command on this distribution. |
| Fedora | same command | for ... in ...; do ... done |
Same command on this distribution. |
| Arch | same command | for ... in ...; do ... done |
Same command on this distribution. |
Keep learning