Compare integer in bash, unary operator expected

前端 未结 4 1446
谎友^
谎友^ 2020-12-04 05:44

The following code gives

[: -ge: unary operator expected

when

i=0

if [ $i -ge 2 ]
then
    #some code
fi
         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-04 06:23

    I need to add my 5 cents. I see everybody use [ or [[, but it worth to mention that they are not part of if syntax.

    For arithmetic comparisons, use ((...)) instead.

    ((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed.

    See: ArithmeticExpression

提交回复
热议问题