3

I've started learning bash scripting. I wrote simple while loop, but it doesn't work. it's say that : command not found.does anybody knows why ? Here is my code:

let x=5; while [$x -lt 10];do echo "x is : $x";let x=$x+1; done
BMW
  • 42,880
  • 12
  • 99
  • 116
user2806363
  • 2,513
  • 8
  • 29
  • 48

1 Answers1

6

Add spaces.

while [ $x -lt 10 ];

For more information, please see this answer to How to use double or single bracket, parentheses, curly braces:

A single bracket ([) usually actually calls a program named [; man test or man [ for more info. Example:

$ VARIABLE=abcdef
$ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi
yes

Also, this is what info test has to say on the matter:

'test' has an alternate form that uses opening and closing square brackets instead a leading 'test'. For example, instead of 'test -d /', you can write '[ -d / ]'. The square brackets must be separate arguments; for example, '[-d /]' does not have the desired effect. Since 'test EXPR' and '[ EXPR ]' have the same meaning, only the former form is discussed below.

Therefore, the equivalent would look like:

let x=5; while test $x -lt 10;do echo "x is : $x";let x=$x+1; done
Community
  • 1
  • 1
  • 1
    Exactly. The conditional `[`...`]` is just a shell command, and like any other command, the command name (`[`) has to be separated from its arguments (`-lt 10 ]`) by space. – Mark Reed Feb 11 '14 at 21:41