syntax of for loop in linux shell scripting

mkab picture mkab · Apr 11, 2011 · Viewed 33k times · Source

I have a problem implementing a for loop. I get this error when I execute my script

test1.sh: 2: Syntax error: Bad for loop variable

I don't understand this error.

This is my script

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..."
done

can any one tell me syntax for for loop in sh(in ubuntu it links to dash shell) shell in ubuntu?

Answer

Michael Krelin - hacker picture Michael Krelin - hacker · Apr 11, 2011

You probably run it with sh, not bash. Try bash test1.sh, or ./test1.sh if it's executable, but not sh test1.sh.