1

I have this code

#!/bin/bash
read num
sum=0
i=1
while [$i -le $num]; do
    let sum=$sum+1
    let i=$i+1
done
echo $sum

I am getting the error ./test: line 5: [1: command not found , but I am not sure what part of this is wrong and can not be found.

Jay
  • 11
  • [ $i -le $num ]. Note the spaces. – Kevin Oct 30 '14 at 00:41
  • The spaces have to be on both sides of the variables and operators? – Jay Oct 30 '14 at 00:43
  • Yes. [ is a command, and it requires ] as its last argument. Note that the assignment must not have a space on either side (as you correctly have it now). – Kevin Oct 30 '14 at 00:45
  • here you go : #!/bin/bash set -x # this will print every step of the excution process

    sum=0 i=1 while [ $i -le "$@" ]; do sum+=1 i+=1 done echo $sum

    – z atef Oct 30 '14 at 01:21

0 Answers0