Help answer threads with 0 replies.
Go Back > Blogs > linux-related notes
User Name


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.
Rate this Entry

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

Originally Posted by cfajohnson
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 ]
echo "------------"

Make sure that $j expands to a value. Best is to set it explicitly, but you can also give it a default value (0 in this example):

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]".
Posted in Uncategorized
Views 478 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 12:31 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration