Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Give a default/non-null value in order to avoid the "unary operator expected" error
Posted 02-19-2014 at 03:00 PM by the dsc
Tags bash
Quote:
Originally Posted by cfajohnson
Quote:
i am trying to compare a value with value 50. but i am getting
"[: -lt: unary operator expected"
I am using
if [ $j -lt 50 ]
then
echo "------------"
fi
"[: -lt: unary operator expected"
I am using
if [ $j -lt 50 ]
then
echo "------------"
fi
Code:
if [ ${j:-0} -lt 50 ]
Zero, literally "0", is not the same as "", but actually a value, and conversely "" isn't "0".
I never understood the problem exactly, I thought that it had to do with not using quotes for some reason, and used to "cheat" with double checks. That thread explains that, when the variable is empty, the result is essentially "[ "" -lt 10 ]". Notice the empty quotes within the check; that's what happens when you add quotes, it really does not help at all would be the same as just "[ -lt 10]".
Total Comments 0