How to make a for loop in command line?
The syntax of a for
loop from the bash
manual page is
for name [ [ in [ word ... ] ] ; ] do list ; done
The semicolons may be replaced with carriage returns, as noted elsewhere in the bash
manual page: "A sequence of one or more newlines may appear in a list instead of a semicolon to delimit commands."
However, the reverse is not true; you cannot arbitrarily replace newlines with semicolons. Your multiline script can be converted to a single line as long as you observe the above syntax rules and do not insert an extra semicolon after the do
:
for i in `seq 1 10`; do echo $i; done
The semicolon after do
is an error and should not be there.
The following works correctly:
for i in `seq 1 10`; do echo $i; done