How to solve “Unary operator expected” errors?


Overview

In this article, we will discuss how to solve the unary operator expected error in Linux. This is one of the most common errors that you may encounter while working with Linux. In this post, we will see how to fix it and what are the possible causes for this error.

What Is Unary Operator Expected?

The unary operator expects is a type of syntax error that occurs when an expression has only one operand. For example −

int x 5; //error - unary operator expected

The above code snippet shows an error because there is no binary operation (operator) between two variables. So, if you have such kind of error in your program then you need to check whether all the expressions in your program have at least two operands or not.

What Causes the Error?

Let's first write a helper function, which checks whether the number entered by a user is equal to one.

$ cat unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
if [ $num1 -eq 1 ]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi

Once we've written our script, we want to test it out.

$ ./unity_check.sh
Enter the input: 5
Not equal to One !!

So far, so well! Let’s see if we can handle the case where the user presses the “return” key without entering any text.

$ ./unity_check.sh
Enter the input:
./unity_check.sh: line 3: [: -eq: unary operator expected
Not equal to One !!

Let’s go through the code step by step to figure out why it doesn't work.

$ bash -xv ./unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
+ read -p 'Enter the input: ' num1
Enter the input:
if [ $num1 -eq 1 ]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi
+ '[' -eq 1 ']'
./unity_check.sh: line 3: [: -eq: unary operator expected
+ echo 'Not equal to One !!'
Not equal to One !!

We can see from the debug log that the if statement evaluates to true.

if [ -eq 1 ]

Due to the empty value in the variable, one of the sides of the comparison expression vanishes. As a result, we're now dealing with just one argument for the equal (=) operator. Therefore, Bash complains with "unary operator expected".

Preventing the Error by Quoting the Variables

To avoid word splittings, we can use double quotes for our variables. So let's update our original bash scripts −

$ cat unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
if [ "$num1" -eq 1 ]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi

Here, we've replaced $num1 with "$num1". Let's now run our tests −

$ ./unity_check.sh
Enter the input: 1
Number entered is 1

It usually functions normally when there isn't any input. Let's check again for the empty input −

$ ./unity_check.sh
Enter the input:
./unity_check.sh: line 3: [: : integer expression expected
Not equal to One !!

We're getting an "int" expression expected message here. Let’s debug the script −

$ bash -xv ./unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
+ read -p 'Enter the input: ' num1
Enter the input:
if [ "$num1" -eq 1 ]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi
+ '[' '' -eq 1 ']'
./unity_check.sh: line 3: [: : integer expression expected
+ echo 'Not equal to One !!'
Not equal to One !!

We can see from the debug outputs that the if statement evaluates to true.

if [ '' -eq 1 ]

And, we're comparing an apple to an orange! We were getting this warning because we're using the "eq" operator to check if two strings are equal. But, the "eq" operator is meant for checking integers only. To check if two strings are equivalent, we should use the "=" operator instead. So let's change our code and try again −

$ cat unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
if [ "$num1" = 1 ]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi
$ ./unity_check.sh
Enter the input: 3
Not equal to One !!

$ ./unity_check.sh
Enter the input: 1
Number entered is 1

$ ./unity_check.sh
Enter the input:
Not equal to One !!

It works as intended for all the test cases.

Using the Double Bracket Syntax to Prevent the Error

We can use the [[ construct in Bash to check if an expression evaluates to true or false.

When using the double bracket syntax, bash doesn't split words when performing expansion. This means that it's more tolerant to cases where variables may not be set.

Let’s modify our script −

$ cat unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
if [[ $num1 -eq 1 ]]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi

Let’s now run our test case for empty inputs.

$ ./unity_check.sh
Enter the input:
Not equal to One !!

Great! It works as expected.

Handling the Variable Not Defined Cases

We've already covered some methods for preventing "Unary Operator Expected" error messages in earlier sections. Now let's look at some methods for handling variables that aren't defined.

Assigning Default Value to a Variable

If we don't define a variable, we can give it a default value −

$ cat unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
if [ "${num:-1}" -eq 1 ]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi

Here, the ${#num:-1} syntax makes sure that the value of the $num varaible will be taken as 1 (the default) if it’ s not defined. And we can verify it by doing some tests −

$ ./unity_check.sh
Enter the input: 4
Not equal to One !!

$ ./unity_check.sh
Enter the input:
Number entered is 1

Using the -z Unary Operator

You can also use the -z operator to check if an argument is empty.

$ cat unity_check.sh
#! /bin/bash
read -p "Enter the input: " num1
if [ -z "$num1" ]
then
   echo "The number is empty"
   exit 0
fi
if [ "${num1}" -eq 1 ]
then
   echo "Number entered is 1"
else
   echo "Not equal to One !!"
fi

If the value of the variables is empty, then we're printing out the relevant statements and terminating the script.

$ ./unity_check.sh
Enter the input:
The number is empty

Conclusion

We looked at several different ways to fix the “unary operand expected” error.

We first discussed what causes this problem, then we looked at different ways to solve it.

We looked at several ways to explicitly handle the case where a variable isn't defined.

Updated on: 26-Dec-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements