Let's take a simple for loop


for i in `seq 1 10`;
    echo $i

AFAIK semicolon in bash scripts makes shell execute current command synchronously and then go to the next one. Pressing enter does literally the same except it doesn't allow you to enter the following command, flushing the buffer immediately.

So why shell can't interpret the following line

for i in `seq 1 10`; do; echo $i; done

how does this for loop actually work?


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
  • 2
    The final script is wrong, as @dr01 pointed out, you need to remove the semicolon after do. – Jake Mar 17 '18 at 19:45
  • 1
    @jake, this also says you must remove the semicolon after do. In addition, this answer explains why you must remove the semicolon after `do'. – user4556274 Mar 17 '18 at 21:31
  • What if $i needs to be part of a file name? Does it need double quotes? – Anonymous Nov 7 '18 at 21:41

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.