Bash script error [: !=: unary operator expected

后端 未结 2 1163
你的背包
你的背包 2020-11-27 03:58

In my script I am trying to error check if the first and only argument is equal to -v but it is an optional argument. I use an if statement but I keep getting the unary oper

2条回答
  •  抹茶落季
    2020-11-27 04:28

    Quotes!

    if [ "$1" != -v ]; then
    

    Otherwise, when $1 is completely empty, your test becomes:

    [ != -v ]
    

    instead of

    [ "" != -v ]
    

    ...and != is not a unary operator (that is, one capable of taking only a single argument).

提交回复
热议问题