2

I wrote a simple script to check if a number is between 20 and 30:

while true 
do
echo 'enter a number between 20 and 30'
 read e
 if  [ $e  -lt 30] && [$e -gt 20 ]
 then
    echo ' you entered desired number' && break
fi
done

but it has a error as follows:

line 5: [: missing `]'

where did I go wrong?

3 Answers 3

4

You need a space here:

if [ $e -lt 30 ] && [ $e -gt 20 ]
              ^      ^
              |      |
              |      |
---------------------

Always try and run the bash script in debug mode. To run your script in debug mode, simply do the following on your command prompt.

bash -x scriptname.sh

2

You need some whitespace around the [ and ]:

if [ $e -lt 30 ] && [ $e -gt 20 ]

Since [ can be a program*, you have to treat it like you're passing in command line arguments.

* I say can be as it might be internalized by some shells

2

you need a space between the number and the bracket. Line 5 should read

if  [ $e  -lt 30 ] && [ $e -gt 20 ]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.