Gaurav Rai
Gaurav Rai

Reputation: 313

Loop variable error while using different syntax for "for loop"

When i'm using for like for i in 1 2 3 4 5 then my file contains

#!/bin/sh at the top

But when i'm using for(( i = 0; i<=5; i++)) then it is showing error

Syntax error: Bad for loop variable

and running properly when I remove shebang.

Please tell me the reason behind this.

Upvotes: 1

Views: 241

Answers (1)

anubhava
anubhava

Reputation: 785156

You need to run with BASH, so use BASH shebang:

#!/bin/bash

Since this arithmetic BASH loop syntax isn't supported in older bourne shell:

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

Upvotes: 5

Related Questions