Bash Unary Operator Expected

bash

Solution

If you want to check for the `null` value for a variable, use the `-z` operator:

if [ -z "${netmask[1]}" ]; then

On example:

VAR=""

if [ -z "$VAR" ]; then
  echo This will get printed
fi

Please note the parentheses around the variable: `"$VAR"`.

Problem

Okay, so within my script (this is my first time working with Bash) I am being met with two unary operator expected errors. The code itself is actually working fine, but it's presenting me with these errors at runtime: [: !=: unary operator expected For the line: ``` if [ ${netmask[1]} != "" ]; do ``` So for the first error, it's thrown when `${netmask[1]}` is `""` (`null`). I have tried multiple ideas and still can't get it to work without returning that error in the process. I solved it by adding quotation marks (grrr) ``` if [ "${netmask[1]}" != "" ]; do ```

Original source

Related problems