Hello, friends. It is very common that Linux users try to learn something about Bash. That’s why today we bring you this short post that will help you to fix the “unary operator expected” error on bash.
When a novice user tries to program in bash, it is possible to encounter all kinds of errors. One of the most common headaches is “Unary Operator Expected”. This error is common and has a very practical solution.
How to solve the “unary operator expected” error?
In short, this error happens when you attempt to compare one empty value with another. For example,
if [ $dist = "Ubuntu" ]
But imagine that $dist
is empty or has no value at all. Internally, this is what the system interprets it to mean
if [ = "Ubuntu" ]
It doesn’t take an expert programmer to realize that this is wrong and that it is not a valid expression.
So, all you have to do is to initialize the variable to be compared or transform it into a data type equal to that of the other field. For example,
if [ "$dist" = "Ubuntu" ]
Or, I repeat, initialize the variable.
Conclusion
Bash programming requires a certain level of knowledge, but being a different language it can cause some errors to novice users.