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?
What does
give on your machine ?
Make
sh
a symbolic link tobash
and then you can dosh ./test1.sh
You probably run it with
sh
, notbash
. Trybash test1.sh
, or./test1.sh
if it's executable, but notsh test1.sh
.Your shell script (as shown) runs in both Korn shell and Bash. Some thoughts:
syntax error: '(' unexpected
instead).for ((x;y;z))
construct.Try this:
set -xv
will display all lines as they are executed.$RANDOM
should display a value if this is either BASH or Kornshell (your for loop will work in either one).{$BASH_VERINFO[x]}
should only be set if this is truly BASH. These aren't even set even if you run Korn shell after you're in BASH (unlike $SHELL which will still containbash
).If the for loop still gives you trouble, just delete it. Somewhere in this script, we'll find out if you're really executing a bash shell or not.
A standard POSIX shell only accepts the syntax
for varname in list
The C-like for-loop syntax
for (( expr1; expr2; expr3 ))
is a bashism.You can get similar behavior in the standard POSIX shell using
for c in $(seq 1 5)