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

**URL:** <https://get-help.theconstruct.ai/t/how-to-fix-or-avoid-bash-unary-operator-expected-on-linux/27239>\
**Category:** Knowledgebase\
**Tags:** unary-operator, bash, linux\
**Created:** [November 7, 2023, 9:06pm UTC](https://get-help.theconstruct.ai/t/how-to-fix-or-avoid-bash-unary-operator-expected-on-linux/27239 "2023-11-07T21:06:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [November 7, 2023, 9:06pm UTC](https://get-help.theconstruct.ai/t/how-to-fix-or-avoid-bash-unary-operator-expected-on-linux/27239/1 "2023-11-07T21:06:53Z")

</div>

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

- copy the given scripts to your local Linux installation, or better still…
- use our [online learning and development environment](https://app.theconstructsim.com/) running right inside your browser!

* * *

#### 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.

```auto
cd ~
touch unary_operator_test.sh

```

1. Make it executable.

```auto
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:

```auto
nano unary_operator_test.sh
# paste the content and save

```

#### `unary_opeator_test.sh`

```auto
#!/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:

```auto
./unary_operator_test.sh

# or

bash unary_operator.sh

```

You should get an output like the following:

```auto
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=""`:

```auto
#!/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:

```auto
bash unary_operator.sh

```

You should now have this output:

```auto
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:

```auto
#!/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.

```auto
bash unary_operator.sh

```

The same error as before:

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

```

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

```auto
bash unary_operator.sh yes

```

Now it works:

```auto
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:

```auto
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:

```auto
#!/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:

```auto
bash unary_operator.sh

```

You should have:

```auto
user:~$ bash unary_operator.sh

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

```

1. Change the script to:

```auto
# 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:

```auto
bash unary_operator.sh

```

Again, you should have:

```auto
user:~$ bash unary_operator.sh

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

```

If you run:

```auto
bash unary_operator.sh yes

```

You should have:

```auto
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:

- ["bash: [: =: unary operator expected" on Linux for Robotics course - Course Support / Linux Basics For Robotics - The Construct ROS Community (robotigniteacademy.com)](https://get-help.robotigniteacademy.com/t/bash-unary-operator-expected-on-linux-for-robotics-course/3257/7)
- [Exe 4.1: unary operator expected error - Course Support / Linux Basics For Robotics - The Construct ROS Community (robotigniteacademy.com)](https://get-help.robotigniteacademy.com/t/exe-4-1-unary-operator-expected-error/5162)
