user862489
user862489

Reputation:

Bash 'for' loop syntax?

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")

Upvotes: 12

Views: 14755

Answers (3)

Michał Šrajer
Michał Šrajer

Reputation: 31182

The portable way is:

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

Upvotes: 11

jman
jman

Reputation: 11586

Replace

for (($i=0...

with

for ((i=0;i<10;i++))

Upvotes: 16

Eric Fortis
Eric Fortis

Reputation: 17350

Another way

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

Upvotes: 7

Related Questions