Harness `seq` for Effortless Numbered Loops
Quick Tip
Harness `seq` for Effortless Numbered Loops
Challenge: You need to perform an action multiple times, and each iteration requires a unique, sequential number. Manually creating these numbers or using more complex loops can be tedious.
The Solution: Utilize the `seq` command to generate sequences of numbers, which can then be used directly in shell loops.
for i in $(seq 1 5); do echo "Processing item number $i"; done
Why it works: The `seq` command generates a sequence of numbers. `seq 1 5` outputs numbers from 1 to 5, each on a new line. The `$(…)` syntax performs command substitution, meaning the output of `seq` is used as the list of items for the `for` loop.
Pro-Tip: You can specify a step value with `seq` for more granular sequences, e.g., `seq 0 2 10` will output 0, 2, 4, 6, 8, 10.
Linux Tips & Tricks | © ngelinux.com | 4/26/2026
