12

What is the syntax for a Bash for loop?

I have tried:

for (($i=0;$i<10;$i ++))
do
    echo $i
done

I get this error:

line 1: ((: =0: syntax error: operand expected (error token is "=0")
1

3 Answers 3

16

Replace

for (($i=0...

with

for ((i=0;i<10;i++))
Sign up to request clarification or add additional context in comments.

1 Comment

@Laurent, reverted as it doesn't fit in with the rest of the answer (replace x by y).
11

The portable way is:

for i in `seq 0 9`
do
    echo "the i is $i"
done

2 Comments

That would loop from 1 to 10 instead of 0 to 9.
@Laurent: Fixed that. Good catch.
7

Another way

for i in {0..9}
  do
    echo $i
  done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.