[Resolved] ‘Unary Operator Expected’ Error in Bash

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The ‘Unary Operator Expected’ error in bash indicates that the command or operator expects a single parameter, but the parameter is not given. The most common way to fix this error is to include a double quotation with the variable instead of a null value, also you can use the double square brackets, or assign a value by default. In this article, you will learn all of those processes. So let’s start!

What Causes the ‘Unary Operator Expected’ Error in Bash Scripting?

The ‘Unary Operator Expected’ error in bash typically occurs when you use an operator or command that expects an argument or operand but doesn’t receive it. For instance, if you use a unary operator like -z (used to check if a string is empty) without providing a string to operate on, you’d get this error. Let’s create a helper script that assesses whether the number provided by the user is equal to 5.

Rewrite the code to check how the unary expected error occurs in bash script:

#!/bin/bash

read -p "Enter a number: " user_input

if [ $user_input -eq 5 ]; then
   echo "You entered the number 5."
else
   echo “The number you entered is not equal to 5”
fi

The script uses a conditional statement with the -eq binary operator within square brackets to compare user_input to 5. The crucial factor here is that if the user_input  is null then, there is a chance to trigger the ‘Unary Operator Expected’ error.

Run the script by using the ./unary_expected.sh command and see how the unary expected error occurs in the bash script.

What Causes the Unary Operator Expected Error in Bash ScriptingIn the picture above, I entered 5 and the code gave me the same value. That means it passed the number to the script correctly. But when I entered a null string, the code gave me an error because it didn’t have an operand to check if the condition was equal. So, I’ll look at different ways to fix this error in the next part of the article.

3 Solutions to Fix ‘Unary Operator Expected’ Error in Bash

To resolve the ‘Unary Operator Expected’ error, you can use the following three solutions:

Solution 1: By Quoting the Variables

Due to the lack of value, the null variable creates a void on the operand to test. So it is advisable to replace the variable with a double quote to avoid the expected error.

Here is a code snippet to avoid the ‘Unary Operator Expected’ error in bash:

#!/bin/bash

read -p "Enter a number: " user_input

#Preventing the Error by Quoting the Variables
if [ "$user_input" == 5 ]; then
   echo "You entered the number 5."
else 
   echo "The number you entered is not 5."
fi

This Bash script captures user input and checks if it’s equal to 5. The user_input variable has been enclosed by a double quote to avoid the ‘Unary Operator Expected’ error in bash. If the user’s input matches 5, it outputs You entered the number 5. Otherwise, it responds with The number you entered is not 5.

To run the script use the ./unary_expected.sh command in your command line.

Fix 'Unary Operator Expected' Error by Quoting the VariablesAs you can see now from the image given above, the code perfectly works for the Null value and no ‘Unary Operator Expected’ error has occurred.

Solution 2: Using the Double Bracket

The use of double brackets [[ … ]] in the conditional statement also plays a vital role in preventing the ‘Unary Operator Expected’ error.

To avoid the ‘Unary Operator Expected’ error in bash, you can use the following command:

#!/bin/bash

read -p "Enter a number: " user_input

#Using the Double Bracket Syntax to Prevent the Error
if [[ $user_input -eq 5 ]]; then
   echo "You entered the number 5."
else 
   echo "The number you entered is not 5."
fi

Here, The double bracket syntax, [[ … ]] provides better handling of variables with spaces or special characters. If the condition is met, it outputs You entered the number 5. Otherwise, it displays The number you entered is not 5.

Use the./double_bracket.sh command to run the script

Prevent the 'Unary Operator Expected' Error Using the Double BracketHere, as you see from the image depicted above, the code works perfectly for regular integer values like 4 and 5. It also returns the output for the null string as well.

Solution 3: By Setting Default Value

As you already know a null string occurs ‘Unary Operator Expected’ error in the bash script. So it will be a better approach if you make sure the variable will always have a value even if a user inserts any null value in it. The best way of doing this is to set the default value to the variable.

To fix the ‘Unary Operator Expected’ error in bash, you can use ${variable_name:-variable_value}. The given syntax will set a default value. Here is the full snippet of it:

#!/bin/bash

# Attempt to read a value from user input
read -p "Enter a value (or press Enter for default): " user_input

# Assign a default value to replace a null string with a dummy number 404, preventing the unary operator expected error
user_input="${user_input:-404}"

# Perform an operation on the variable
if [ "$user_input" -eq 1 ]; then
   echo "You entered the number 1."
else
   echo "The value you entered is not equal to one."
fi

The Bash script initiates by prompting the user for input, with the option to press Enter for a default value. The default value, 404, is assigned to the user_input variable using the ${user_input:-404} syntax. This ensures that the variable is never null, thereby preventing the error.

Enter the ./default.sh command to run the code

 Setting Default ValueAs the image shows, the script successfully handles the possible error by incorporating a dummy integer as a default value.

Utilizing the “-z” Unary Operator to Avoid the ‘Unary Operator Expected’ Error

To avoid the ‘Unary Operator Expected’ error in a Bash script, you can use the -z unary operator and check whether the given user input is empty or not. The -z unary operator will check if a variable is empty or has a zero length.

The following code will set a default value to avoid ‘Unary Operator Expected’ error:

#!/bin/bash

read -p "Enter a value: " user_input

#Using the -z Unary Operator to prevent the unary operator expected error in bash
if [ -z "$user_input" ]; then
   user_input=404
fi

if [ "$user_input" -eq 1 ]; then
   echo "You entered the number 1."
else
   echo "The value you entered is not equal to one."
fi

The -z operator checks if the user_input is empty or null. If it is, it assigns the default value 404 to user_input. This prevents the ‘Unary Operator Expected’ error that could occur when a null variable is used in a binary operator.

The script then proceeds to check whether user_input is equal to 1 using the -eq binary operator. If the condition is met, it prints “You entered the number 1.” Otherwise, it outputs “The value you entered is not equal to one.”

Run the ./z_operator.sh command to execute the file to the command line

Utilizing the “-z” Unary OperatorNow as you can see initially, the code makes judgments based on the integer value. However, the code successfully handles a null string by incorporating the -z operator to check the variable length and place a default value thereafter. The code returns “The value you entered is not equal to one” based on a null input.

Conclusion

To sum up, any scriptwriter needs to know how to deal with ‘Unary Operator Expected’ errors when using Bash Scripting. Even though it can be confusing at first, it’s all just a small part of the process once you get the hang of it. However, If you have any questions or comments about this article, please let us know! Thanks!

People Also Ask

Which logical operator is unary?

The logical NOT (!) operator is the unary logical operator. It operates on a single operand, negating its truth value. For example, !condition is a unary usage where ! negates the truth of the condition. Other common logical operators, such as logical AND (&&) and logical OR (||), are typically binary operators, requiring two operands to perform comparisons and return boolean results.

Which is the correct example of a unary operator & ==?

The correct example of a unary operator is !. The exclamation mark (!) is a unary logical NOT operator, which negates the value of a single operand. It is used to invert the truth value of a condition.

On the other hand, == is not a unary operator; it’s a binary operator used for equality comparison in various programming languages but not for negating a single operand.

How is unary operator used?

In Bash, unary operators perform operations on variables, particularly numerical values. The unary plus operator (+) indicates a positive numeric value, while the unary minus operator (-) is used to negate a numeric value, changing it from positive to negative or vice versa.

How do I use unary plus?

In Bash, the unary plus (+) operator is used to indicate a positive numerical value. It doesn’t have any direct impact on the value itself, but it can be useful for clarity in certain situations. For example, when assigning a positive number to a variable, you can use the unary plus to make the number positive.

What is unary minus?

The unary minus, represented by the minus symbol (), is an operator used to change the sign of a numeric value from positive to negative or vice versa. It is commonly used in arithmetic to negate a number, making it the opposite sign. For example, applying the unary minus to the number 5 results in -5.

Is Asterisk an unary operator?

No, the asterisk (*) is not an unary operator. In programming, the asterisk is often used as a binary operator for multiplication, not for changing the sign or performing unary operations on a single value. Unary operators typically act on a single operand, while binary operators operate on two operands.

Is sin a unary operator?

Yes, the sin function, as used in trigonometry and mathematics, is considered a unary operator. It takes a single input, which is an angle in radians, and returns the sine of that angle. Unary operators operate on a single operand, and sin is a prime example of a unary operator in mathematics.

Related Articles

<< Go Back to Unary Operators in Bash | Bash Operator | Bash Scripting Tutorial

5/5 - (4 votes)
Mohammad Shah Miran

Hey, I'm Mohammad Shah Miran, previously worked as a VBA and Excel Content Developer at SOFTEKO, and for now working as a Linux Content Developer Executive in LinuxSimply Project. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). As a part of my job, i communicate with Linux operating system, without letting the GUI to intervene and try to pass it to our audience.

Leave a Comment