-3

I wrote this simple script:

i = "1";
b = "4";
if [ "$i"-gt "$b" ]
then
echo ""
fi

But I got this error on attempting to run it:

[: -gt: unary operator expected

What is wong ?

3

Please provide complete error listings as you ask ;-)

The program 'i' is currently not installed. You can install it by typing:
sudo apt-get install iprint
b: command not found
bash: [: -gt: unary operator expected

This has only subtle differences, but works; study it:

i="1";
b="4";
if [ "$i" -gt "$b" ]
then
  echo "Greater"
else
  echo "NOT Greater"
fi
  1. i = "1" is interpreted by bash to be run i with the parameters = and "1".
  2. "$i" is has not been set to anything due to 1), i.e. it is "" (null string) - the same with "$b"

This makes [ "$i"-gt "$b" ] read as [ -gt ] as interpretation comes to that point. Also note that as you fix 1) you still have the issue of 'no space' between "$i" and -gt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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