What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Bash Loop Error

Col8eral

Regular Contributor
Try to run a simple loop script

Code:
#!/bin/sh
max=10
for (( i=2; i <= $max; ++i ))do
echo"$i"
done

but im getting "syntax error: bad for loop variable" when I run it. Wht am I doing wrong?
Thanks.
 
Try to run a simple loop script

Code:
#!/bin/sh
max=10
for (( i=2; i <= $max; ++i ))do
echo"$i"
done

but im getting "syntax error: bad for loop variable" when I run it. Wht am I doing wrong?
Thanks.
Just a quick clarification. Our routers use POSIX, not Bash. (You can enable it if you so choose, but it gets more messy). POSIX shell has issues with advanced loops like this... you probably want to use something like this:

Code:
i=0
while [ $i -le 10 ]; do
    echo "$i"
    i=$(( i + 1 ))
done
 
Last edited:
Thank you. I tried that and got this error:
loop: line 7: syntax error: unexpected end of file (expecting "do")
Make sure set the environment at the top:

Code:
#!/bin/sh

i=0
while [ $i -le 10 ]; do
    echo "$i"
    i=$(( i + 1 ))
done

exit 0

This works for me:

1742133794017.png
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top