# problem with while loop

**URL:** <https://community.unix.com/t/problem-with-while-loop/275864>\
**Category:** Shell Programming and Scripting\
**Created:** [October 22, 2010, 12:55am UTC](https://community.unix.com/t/problem-with-while-loop/275864 "2010-10-22T00:55:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![angel12345](https://community.unix.com/letter_avatar/angel12345/32/5_5575768a8748004e209b776fc1b2916d.png) [@angel12345](https://community.unix.com/u/angel12345)\
**Post date:** [October 22, 2010, 12:55am UTC](https://community.unix.com/t/problem-with-while-loop/275864/1 "2010-10-22T00:55:31Z")

</div>

hi  
I had created a while loop in a script file called whiletest.sh as follows

as follows:

count=0  
max=10  
while [$count -lt $max]  
do  
echo $count  
echo count=$(count+1)" "  
done  
echo "value of count:$count"

then i run the script with the command

sh whilestest.sh  
but its giving me an error like

Xpath-\>home\> sh whiletest.sh  
whiletest.sh[3]: [0: not found.  
value of count:0

please give suggestions to solve this

regards  
Angel

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [October 22, 2010, 1:28am UTC](https://community.unix.com/t/problem-with-while-loop/275864/2 "2010-10-22T01:28:28Z")

</div>

Hi, try:

```nohighlight
# | |
# | spaces |
# | |
# V V
while [$count -lt $max]

```

```nohighlight
count=$((count+1))" "

```

---

<div class="post-metadata">

**Author:** ![angel12345](https://community.unix.com/letter_avatar/angel12345/32/5_5575768a8748004e209b776fc1b2916d.png) [@angel12345](https://community.unix.com/u/angel12345)\
**Post date:** [October 22, 2010, 2:02am UTC](https://community.unix.com/t/problem-with-while-loop/275864/3 "2010-10-22T02:02:29Z")

</div>

i havnt put space in while loop.Then i put the brackets as you told.

```nohighlight
count=0
max=10
while [$count -lt $max]
do
 echo $count
 echo count=$((count+1))" "
 done
 echo "value of count:$count"    
                                                                            
"whiletest.sh" 8 lines, 123 characters 

Xpath->/home> sh whiletest.sh
whiletest.sh[3]: [0: not found.
value of count:0

```

---

<div class="post-metadata">

**Author:** ![michaelrozar17](https://community.unix.com/user_avatar/community.unix.com/michaelrozar17/32/1975_2.png) [@michaelrozar17](https://community.unix.com/u/michaelrozar17)\
**Post date:** [October 22, 2010, 2:13am UTC](https://community.unix.com/t/problem-with-while-loop/275864/4 "2010-10-22T02:13:58Z")

</div>

The space should in between the test command i.e [$count -lt $max] and not like [$count -lt $max]. And the below expression does not work as its not the right syntax

```nohighlight
echo count=$(count+1)" " # not correct
Try as suggested by Scrutinizer: count=$((count+1))" " 

```

or try

```nohighlight
count=0
max=10
while [$count -lt $max]
do
	echo $count
	let count=$count+1
done
echo "value of count:$count"

```

---

<div class="post-metadata">

**Author:** ![frans](https://community.unix.com/user_avatar/community.unix.com/frans/32/1697_2.png) [@frans](https://community.unix.com/u/frans)\
**Post date:** [October 22, 2010, 2:16am UTC](https://community.unix.com/t/problem-with-while-loop/275864/5 "2010-10-22T02:16:42Z")

</div>

> [@angel12345](#):
>
> i havnt put space in while loop.Then i put the brackets as you told.
> 
> ```plaintext
> ...
> while [$count -lt $max]
> ...
> 
> ```

You MUST put spaces in the '[]' test statement like `while [$count -lt $max]`

---

<div class="post-metadata">

**Author:** ![angel12345](https://community.unix.com/letter_avatar/angel12345/32/5_5575768a8748004e209b776fc1b2916d.png) [@angel12345](https://community.unix.com/u/angel12345)\
**Post date:** [October 22, 2010, 2:24am UTC](https://community.unix.com/t/problem-with-while-loop/275864/6 "2010-10-22T02:24:59Z")

</div>

sorry Scrutinizer, i mistook ur comment 🙂  
regards  
Angel

---------- Post updated at 01:24 AM ---------- Previous update was at 01:22 AM ----------

hi ,  
thanks to all of uuuuu its worked perfectlyyyy 🙂  
special tahnks Scrutinizer for immediate replyyyy
