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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.