Yes, you can write a Bash for loop entirely on one line by separating each part with semicolons: for i in 1 2 3; do echo $i; done. This compact form works in any Bash shell, including scripts and interactive terminals, and is perfect for quick, ad‑hoc commands.
Formula
The one‑line for loop follows the same syntax as a multi‑line loop, just compressed:
for variable in list; do commands; done
- variable – a name you choose to hold each item from the list (e.g.,
i). - list – a space‑separated set of items (words, numbers, filenames, or command output).
- commands – any Bash commands to run for each item.
;– separates the parts. The finaldoneends the loop.
You can also use the C‑style form: for ((i=1; i<=5; i++)); do echo $i; done.
Worked Example
Let’s run a simple one‑liner that prints the numbers 1 to 3:
for i in 1 2 3; do echo $i; done
Step by step:
- Bash sets
i=1and runsecho 1. - Then
i=2, andecho 2runs. - Finally
i=3, andecho 3runs.
Output:
1 2 3
Exactly what you would get with a multi‑line version.
When to Use a One‑Liner
One‑line loops shine in interactive shells and quick scripts. Use them when you need to:
- Run the same command on several files:
for f in *.txt; do wc -l $f; done - Generate a series of numbers:
for n in {1..10}; do echo $n; done - Loop over command output:
for user in $(cut -d: -f1 /etc/passwd); do echo $user; done - Test a command quickly without writing a script.
For anything longer than a few lines, a multi‑line script is easier to read and maintain. One‑liners are best for short, immediate tasks.
How to Read the Result
The loop executes the commands once for each item in the list. The output is whatever those commands produce. If you are printing values, you see one line per iteration. If you are running commands that modify files, you see no output unless you add echo or the command itself prints something.
For example, for f in *.log; do gzip $f; done compresses each .log file silently. To see what is happening, add echo: for f in *.log; do echo "Compressing $f"; gzip $f; done.
Common Mistakes and Pitfalls
Several errors trip up beginners:
- Missing semicolon before
do. The correct form isfor i in 1 2; do ...; done. Forgetting the semicolon gives a syntax error. - Spaces around the equals sign in C‑style loops.
for ((i=1; i<=5; i++))works, butfor ((i = 1; i <= 5; i++))also works – just don’t put spaces around the=in a normal variable assignment. - Using
;instead of;;incasestatements inside the loop. That’s a different construct. - Quoting issues. If your list items contain spaces, quote them or use an array:
for f in "file name.txt"; do ...; done. - Forgetting
done. Without it, Bash thinks the loop never ends.
One‑Liner vs. Multi‑Line Script
Both forms execute identically. The choice is about readability and context:
- One‑liner – good for quick commands in the terminal, for loops in
cronjobs, or for embedding in a single command line. - Multi‑line – better for scripts you will reuse, debug, or share. It is easier to add comments and see the structure.
You can convert a one‑liner to multi‑line by replacing semicolons with newlines. For example:
for i in 1 2 3 do echo $i done
Alternatives to the for Loop
Depending on the task, other tools may be simpler:
whileloop – use when you do not know the number of iterations in advance.findwith-exec– for operating on files that match criteria.xargs– to pass many arguments to a command without an explicit loop.mapfile(orreadarray) – for reading lines from a file into an array, then looping over it.seqor brace expansion – to generate number lists without a loop.
Frequently Asked Questions
Can I use a one‑line for loop in a script file?
Yes, absolutely. A one‑liner is just a compressed version of a multi‑line loop. You can put it in a .sh file and run it like any other script. It will behave exactly the same.
How do I loop over a range of numbers in one line?
Use brace expansion: for i in {1..10}; do echo $i; done. Or use seq: for i in $(seq 1 10); do echo $i; done. For a C‑style loop: for ((i=1; i<=10; i++)); do echo $i; done.
How do I loop over files with spaces in their names?
The safest way is to use a while loop with find or use an array. For a one‑liner, you can set the internal field separator (IFS) to a newline: IFS=$'\n'; for f in $(find . -name '*.txt'); do echo "$f"; done. Better yet, use find ... -exec or a while read loop.
What is the difference between for i in ... and for ((...))?
The first iterates over a list of items (words, filenames, etc.). The second is a C‑style loop that uses arithmetic expressions for initialization, condition, and increment. Use the list form when you already have the items; use the C‑style form when you need a counter with a numeric condition.
Can I nest one‑line for loops?
Yes, you can nest them. Just put the inner loop inside the outer loop’s command list, separated by semicolons. For example: for i in 1 2; do for j in a b; do echo $i$j; done; done. Be careful with quoting and readability – nesting one‑liners quickly becomes hard to read.
Why do I get a syntax error near done?
Most often you forgot a semicolon before do or done. Check that the structure is for ...; do ...; done. Also make sure there are no stray quotes or unmatched parentheses.
Related
Formula used
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 | 100000 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It