Why do I keep getting the unary operator expected error?

Why do I keep getting the unary operator expected error?

I use an if statement but I keep getting the unary operator expected error. I should be more specific: This part of the script above is checking an optional argument and then after, if the argument is not entered, it should run the rest of the program. Quotes! Otherwise, when $1 is completely empty, your test becomes:

When to use the unary operator in Bash?

Otherwise, when $1 is completely empty, your test becomes: …and != is not a unary operator (that is, one capable of taking only a single argument). Or for what seems like rampant overkill, but is actually simplistic Pretty much covers all of your cases, and no empty string or unary concerns.

Can a unary operator take only one argument?

Quotes! Otherwise, when $1 is completely empty, your test becomes: …and != is not a unary operator (that is, one capable of taking only a single argument). Or for what seems like rampant overkill, but is actually simplistic

Why is there an error when log level is not defined?

That error is because LOG_LEVEL is not defined. You haven’t given us any information at all about how you’re running this function (nor what shell you are using or what operating system) but the error will go away if LOG_LEVEL is set.

Why does Bash print the error unary operator expected?

The operator == is a binary operator because it compares two values (arguments), the one on the left side of the operator with the one on the right side. But, what happens when the value of the variable DAY is empty? As you can see the left side of the comparison expression disappears and that’s why Bash prints the error “unary operator expected”.

When to use unary and binary operators in Bash?

Unary operators apply to one argument and are often used to verify the status of a file (e.g. does a specific file exist?). Binary operators apply to two arguments and are used, for example, as part of string or arithmetic comparisons (e.g. is a bigger than b?). Now the error we are seeing makes a bit more sense.