How to fix or avoid “bash: [: =: unary operator expected” on Linux

This error typically happens when you are using the comparison operator with an if statement in bash, and you did not write the statement correctly!

We’ll look at

  1. Why this error happens,
  2. two ways it can happen, and
  3. how you can fix (and better still, prevent) it.

You can


Step 1: Create a test script

First, let’s create a test script that we will use for checking each of these reasons.

  1. Create a bash file in your workspace.
cd ~
touch unary_operator_test.sh
  1. Make it executable.
chmod +x unary_opeator_test.sh
  1. Copy and paste the content below into the file unary_operator_test.sh, using nano or an IDE:
nano unary_operator_test.sh
# paste the content and save

unary_opeator_test.sh

#!/bin/bash

PROMPT="yes"
# capture the output of the command to OUT. 
# "2>&1" ensures we capture error output too
OUT=`if [ $PROMPT  = "yes" ]; then echo "Prompt is yes!"; fi 2>&1`

# print the output of the command
echo $OUT

if [[ "$OUT" = *"unary"* ]]; then 
    # A unary error occurred, exit with error
    exit 1
elif [[ "$OUT" = *"yes"* ]]; then
    # Output is 'Prompt is yes!', exit normally
    exit 0
fi

# No unary error, but PROMPT is not "yes"
echo "PROMPT is not 'yes', but we have no error!"

Step 2: Test the script

Run:

./unary_operator_test.sh

# or

bash unary_operator.sh

You should get an output like the following:

user:~$ bash unary_operator.sh
Prompt is yes!

So it worked, but it will fail sometimes. We’ll see how it can fail in the next section.

Step 3: Make the script fail. Yes, you read that correctly!

  1. Change the code so that PROMPT="":
#!/bin/bash

PROMPT=""
# capture the output of the command to OUT. 
# "2>&1" ensures we capture error output too
OUT=`if [ $PROMPT  = "yes" ]; then echo "Prompt is yes!"; fi 2>&1`

# print the output of the command
echo $OUT

if [[ "$OUT" = *"unary"* ]]; then 
    # A unary error occurred, exit with error
    exit 1
elif [[ "$OUT" = *"yes"* ]]; then
    # Output is 'Prompt is yes!', exit normally
    exit 0
fi

# No unary error, but PROMPT is not "yes"
echo "PROMPT is not 'yes', but we have no error!"

Run the script again:

bash unary_operator.sh

You should now have this output:

user:~$ bash unary_operator.sh
unary_operator.sh: line 6: [: =: unary operator expected

Gotcha, we have the error! Why? Let’s find out another way it could happen. After that, we summarize.

  1. Change the script again, to the following:
#!/bin/bash

# assign the first argument passed to the script to PROMPT
PROMPT=$1

# capture the output of the command to OUT. 
# "2>&1" ensures we capture error output too
OUT=`if [ $PROMPT  = "yes" ]; then echo "Prompt is yes!"; fi 2>&1`

# print the output of the command
echo $OUT

if [[ "$OUT" = *"unary"* ]]; then 
    # A unary error occurred, exit with error
    exit 1
elif [[ "$OUT" = *"yes"* ]]; then
    # Output is 'Prompt is yes!', exit normally
    exit 0
fi

# No unary error, but PROMPT is not "yes"
echo "PROMPT is not 'yes', but we have no error!"

Run it again.

bash unary_operator.sh

The same error as before:

user:~$ bash unary_operator.sh
unary_operator.sh: line 6: [: =: unary operator expected

But wait…before you give up, run the script differently now:

bash unary_operator.sh yes

Now it works:

user:~$ bash unary_operator.sh yes
Prompt is yes!

But, why? Let’s summary the lessons in the next and final section.

Step 4: Understand why the error happens

As you might have noticed, this error happens whenever $PROMPT is not set. That is, when it’s equal to an empty string "" or not defined.

In this case, line 5 becomes:

if [  = "yes" ]; then 

and this causes the error to happen because it’s incomplete.

Let see why the script worked and failed in Step 3.

  • In Step 3 (1), the PROMPT was set to “yes”, so it worked.
  • In Step 3 (2), the PROMPT was set to “”, so there was an error.
  • In Step 3 (3),
    • The PROMPT was set to “” initially, because we did not pass any argument to the script, so the error happened.
    • Then we passed an argument “yes” to the script, and it worked.

Step 5: How can we prevent the “unary error” from appearing?

Quote the variable $PROMPT on line 5. That simple.

Let’s repeat Step 3 (2) and Step 3 (3) with $PROMPT quoted.

  1. Change the script to:
#!/bin/bash

PROMPT=""
# capture the output of the command to OUT. 
# "2>&1" ensures we capture error output too
OUT=`if [ "$PROMPT"  = "yes" ]; then echo "Prompt is yes!"; fi 2>&1`

# print the output of the command
echo $OUT

if [[ "$OUT" = *"unary"* ]]; then 
    # A unary error occurred, exit with error
    exit 1
elif [[ "$OUT" = *"yes"* ]]; then
    # Output is 'Prompt is yes!', exit normally
    exit 0
fi

# No unary error, but PROMPT is not "yes"
echo "PROMPT is not 'yes', but we have no error!"

Then run:

bash unary_operator.sh

You should have:

user:~$ bash unary_operator.sh

PROMPT is not 'yes', but we have no error!
  1. Change the script to:
# assign the first argument passed to the script to PROMPT
PROMPT=$1

# capture the output of the command to OUT. 
# "2>&1" ensures we capture error output too
OUT=`if [ "$PROMPT"  = "yes" ]; then echo "Prompt is yes!"; fi 2>&1`

# print the output of the command
echo $OUT

if [[ "$OUT" = *"unary"* ]]; then 
    # A unary error occurred, exit with error
    exit 1
elif [[ "$OUT" = *"yes"* ]]; then
    # Output is 'Prompt is yes!', exit normally
    exit 0
fi

# No unary error, but PROMPT is not "yes"
echo "PROMPT is not 'yes', but we have no error!"

Then run:

bash unary_operator.sh

Again, you should have:

user:~$ bash unary_operator.sh

PROMPT is not 'yes', but we have no error!

If you run:

bash unary_operator.sh yes

You should have:

user:~$ bash unary_operator.sh
Prompt is yes!

6. Wrap up and consolidate

  • Did you understand why not quoting the variable in the if statement on line 5 causes the “unary operator”?
  • What would happen if you misspelled $PROMPT on line 5, writing it as $PRIMPT for instance? Try it out, not quoting variable at first, and then quoting it.

Reference posts:

1 Like