A Unary operator expected
error in a Bash script usually occurs in artihmetic operations where the script does not find the amount of numbers (or “unary operators”) it expected to.
In this tutorial, you will see a few examples of what causes the Unary operator expected
error, and suggestions on how to fix it.
In this tutorial you will learn:
- What is a
Unary operator expected
error - What causes the
Unary operator expected
error - How to prevent the error with double bracket syntax

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | Bash shell (installed by default) |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Bash script: Unary operator expected
Let’s start with the word unary. This is a word that probably does not enter many people’s daily vocabulary. Therefore it can be confusing and unhelpful to see the word on your screen, without any hints on how to fix the error.
The word unary is basically synonymous with “single.” In the context of mathematics, this could be a single number or other component of an equation.
So, when Bash says that it is expecting a unary operator, it is just saying that you are missing a number in the script.
The error will look something like this:
$ ./test.sh ./test.sh: line 6: [: 1: unary operator expected
As you can see, Bash already gives us some helpful information about how to troubleshoot the error, by giving us the line number where it encountered the problem.
Here is a test script that generated the error on our system:
#!/bin/bash
num1="1"
num2=""
if [ $num1 -eq $num2 ]; then
echo "they are equal"
fi
Do you see the problem? Our if
statement is trying to compare two numbers to see if they are equal, but there is no number stored in the $num2
variable. Since we are using the -eq
operator, which is used to test if two integers are equal, Bash is expecting to find two numbers to compare. When it only finds one, it tells us that it was expecting another unary operator.

The obvious fix here is that we should add a number to our variable. Another way we could prevent this error is by using double brackets in our if
statement.
#!/bin/bash
num1="1"
num2=""
if [[ $num1 -eq $num2 ]]; then
echo "they are equal"
fi
The double brackets
[[ ]]
syntax does not give us the unary operator expected
error because word splitting and path expansion are not used on strings in the double brackets.
The disadvantage of double brackets is that it will not work with every shell, but if you are only working with Bash scripts, then there should be no problem.
If you are trying to compare strings in Bash, or want to test if a string is empty or not, see our other tutorial on Bash script: String comparison examples.
Closing Thoughts
In this tutorial, we saw how to troubleshoot the Unary operator expected
error in a Bash script on Linux. The error can be a bit confusing because of the mathematical jargon instead of an easily understandable explanation, but in the end it is just a fancy way of telling you that the Bash script was expecting another number and instead found something else. Easy fix.