# for loop syntax trouble

**URL:** <https://community.unix.com/t/for-loop-syntax-trouble/192818>\
**Category:** Shell Programming and Scripting\
**Created:** [April 12, 2008, 11:33am UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818 "2008-04-12T11:33:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![visitorQ](https://community.unix.com/letter_avatar/visitorq/32/6_449c533f586939b6e5f246632a46421f.png) [@visitorQ](https://community.unix.com/u/visitorQ)\
**Post date:** [April 12, 2008, 11:33am UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/1 "2008-04-12T11:33:07Z")

</div>

i don't get what's wrong here. i'm writing a shell script that takes 1 argument (a number) from the command-line, but it's throwing an error:  
Syntax error: Bad for loop variable

doesn't make much sense

```nohighlight
for (( i = 1; i = ${1}; i++ )) # error points to this line everytime
do
    echo "Welcome $i times"
done

```

i've tried changing it several times, but it always throws the same error. what's the problem with it?

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [April 12, 2008, 11:50am UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/2 "2008-04-12T11:50:14Z")

</div>

What syntax is that supposed to be? Even in C shell syntax this doesn't make much sense.

The shell doesn't have i++ or that particular variant of for loop syntax. You can construct a loop but the syntax is more like

```nohighlight
i=1
while [$i != $1]; do
  echo Welcome $i times
  i=`expr $i + 1`
done

```

Modern shells have more support for arithmetic, but this should work even in good ole Bourne Shell Classic.

---

<div class="post-metadata">

**Author:** ![visitorQ](https://community.unix.com/letter_avatar/visitorq/32/6_449c533f586939b6e5f246632a46421f.png) [@visitorQ](https://community.unix.com/u/visitorQ)\
**Post date:** [April 12, 2008, 12:13pm UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/3 "2008-04-12T12:13:13Z")

</div>

thank you so much for the quick reply buddy! i ended up doing this which worked just fine:

```nohighlight
#!/bin/sh

for i in `seq 1 ${1}`
do
    echo "Welcome ${i} times";
done

```

but your code seems to be cleaner and make more sense, so i'll adopt that method instead.

---

<div class="post-metadata">

**Author:** ![reborg](https://community.unix.com/user_avatar/community.unix.com/reborg/32/167_2.png) [@reborg](https://community.unix.com/u/reborg)\
**Post date:** [April 12, 2008, 12:16pm UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/4 "2008-04-12T12:16:26Z")

</div>

That loop structure is a prefectly valid construct in bash, ksh93, jsh or zsh however the logic is broken.

a single = is used as an assignment.  
a double = ( == ) is used as comparison.

If you want to print $i times the logic doing the test in the loop should be \<=, or !=.

```nohighlight
for (( i = 1 ; i <= ${1} ; i++ ))

```

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [April 12, 2008, 12:18pm UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/5 "2008-04-12T12:18:50Z")

</div>

This for syntax is valid with bash (the equal compate operator is == not =):

```nohighlight
for (( i = 1; i <= ${1}; i++ )) 
do
    echo "Welcome $i times"
done

```

With sh or ksh, you must use a while statement.

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![visitorQ](https://community.unix.com/letter_avatar/visitorq/32/6_449c533f586939b6e5f246632a46421f.png) [@visitorQ](https://community.unix.com/u/visitorQ)\
**Post date:** [April 12, 2008, 1:46pm UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/6 "2008-04-12T13:46:02Z")

</div>

wow thanks for all the replies! i learned a lot!

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [April 12, 2008, 1:48pm UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/7 "2008-04-12T13:48:59Z")

</div>

Hi.

> [@aigles](#):
>
> ... With sh or ksh, you must use a while statement.
> 
> Jean-Pierre.

This version of ksh accepts the extended for syntax:

```plaintext
#!/bin/ksh

# @(#) s1 Demonstrate for loop in ksh.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)

N=3

echo
for (( i = 1; i <= ${N}; i++ ))
do
    echo "Welcome $i times"
done

exit $?

```

producing:

```plaintext
$ ./s1

(Versions displayed with local utility "version")
Linux 2.6.24-1-686
ksh 93s+

Welcome 1 times
Welcome 2 times
Welcome 3 times

```

I will say I was surprised to find this AT&T version of ksh in Debian Lenny rather than the previous pdksh, but it makes life easier in some situations. Solaris 10 still has pdksh as the default ksh [edit: this is not true, see post below] ... cheers, drl

---

<div class="post-metadata">

**Author:** ![reborg](https://community.unix.com/user_avatar/community.unix.com/reborg/32/167_2.png) [@reborg](https://community.unix.com/u/reborg)\
**Post date:** [April 12, 2008, 3:36pm UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/8 "2008-04-12T15:36:28Z")

</div>

> [@drl](#):
>
> ksh 93s+

See my post below, that syntax is valid in ksh93.

> [@drl](#):
>
> Solaris 10 still has pdksh as the default ksh ... cheers, drl

That is incorrect. Solaris does not and never has had pdksh. The version of ksh in Solars 10 is the official ksh88.

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [April 12, 2008, 5:04pm UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/9 "2008-04-12T17:04:26Z")

</div>

Hi.

> [@reborg](#):
>
> ... The version of ksh in Solars 10 is the official ksh88.

Quite right, thanks for the heads-up -- my method for obtaining the version of ksh did not take SunOS adequately into account.

The version 88i does not recognize the extended for statement. Re-running my script s1 in the earlier post under Solaris 10 produces:

```plaintext
$ ./s1

(Versions displayed with local utility "version")
SunOS 5.10
ksh M-11/16/88i

./s1[12]: syntax error at line 12 : `((' unexpected

```

So, pdksh 5.2.14 99/07/13.2 and ksh 88i do not recognize the extended for loop syntax, but ksh 93s+ does ... cheers, drl

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [April 13, 2008, 2:53am UTC](https://community.unix.com/t/for-loop-syntax-trouble/192818/10 "2008-04-13T02:53:13Z")

</div>

Sorry for the tone of my post, I didn't mean to flame and obviously I didn't know what I was talking about. Looks like I need to examine the Bash manual more closely.
