Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to solve "Unary operator expected" errors?
The "unary operator expected" error is a common syntax error in Bash scripting that occurs when a conditional expression lacks the required operands. This typically happens when variables are empty or undefined, causing the shell to receive fewer arguments than expected for comparison operations.
What Causes the Error?
The error occurs when Bash encounters a comparison operator with missing or empty operands. Let's examine a script that demonstrates this issue
#!/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
When we run this script and press Enter without input, we get
$ ./unity_check.sh Enter the input: ./unity_check.sh: line 3: [: -eq: unary operator expected Not equal to One !!
Using debug mode reveals the problem
+ '[' -eq 1 ']' ./unity_check.sh: line 3: [: -eq: unary operator expected
Due to the empty variable, the comparison becomes [ -eq 1 ] instead of [ value -eq 1 ], leaving the -eq operator without a left operand.
Solution 1 Quote Variables
The first solution is to quote variables to prevent word splitting
#!/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
However, this still produces an error for empty input because -eq expects numeric values
$ ./unity_check.sh Enter the input: ./unity_check.sh: line 3: [: : integer expression expected
For string comparisons, use the = operator instead
#!/bin/bash read -p "Enter the input: " num1 if [ "$num1" = 1 ] then echo "Number entered is 1" else echo "Not equal to One !!" fi
Solution 2 Double Bracket Syntax
Bash's double bracket syntax [[ ]] is more robust and handles empty variables gracefully
#!/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
The double bracket construct doesn't perform word splitting, making it more tolerant of unset or empty variables.
Solution 3 Default Values
You can assign default values using parameter expansion
#!/bin/bash
read -p "Enter the input: " num1
if [ "${num1:-0}" -eq 1 ]
then
echo "Number entered is 1"
else
echo "Not equal to One !!"
fi
The syntax ${num1:-0} uses 0 as the default value if num1 is empty or unset.
Solution 4 Check for Empty Variables
Use the -z operator to explicitly test for empty variables
#!/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
Common Solutions Summary
| Method | Syntax | Best For |
|---|---|---|
| Quote Variables | [ "$var" = value ] |
String comparisons |
| Double Brackets | [[ $var -eq value ]] |
Numeric comparisons |
| Default Values | [ "${var:-default}" -eq value ] |
When defaults make sense |
| Empty Check | [ -z "$var" ] |
Explicit empty handling |
Conclusion
The "unary operator expected" error occurs when Bash comparison operators lack the required operands due to empty or unset variables. The most reliable solutions are using double bracket syntax for numeric comparisons and properly quoting variables for string comparisons.
