"bash: [: =: unary operator expected" on Linux for Robotics course

Hello,

I get the following message when I execute the following …how to clear this

user:~/catkin_ws/src/linux_course_files/move_bb8_pkg/my_scripts$ ./bash_script.sh
./bash_script.sh: line 5: [: ==: unary operator expected
./bash_script.sh: line 8: [: ==: unary operator expected
./bash_script.sh: line 11: [: ==: unary operator expecte

Hi @bandu2356,

it seems that you are referring to an empty variable without escaping it.
I could reproduce your error with the following code:

empty_var=
if [ $empty_var  = "value" ]; then echo condition ok; fi

which gives me:

bash: [: =: unary operator expected

If we just put $empty_var between double quotes "$empty_var", then it works:

if [ "$empty_var"  = "value" ]; then echo Condition ok; fi

This will not give an error.

After making the changes as mentioned by you i am getting this error
/dynamic.sh: line 5: [ : command not found
./dynamic.sh: line 8: [ : command not found
./dynamic.sh: line 11: [ : command not found

Hi @ramku31,

then you probably forgot to put the “if” before the “[”. Are you still having the problem?

If so, could you send me your code as a private message?

I too have the same issue that @bandu2356 has. IDK what to do to rectify it…

Hi @azeem00716,

have you tried the solution proposed above?

it seems that you are referring to an empty variable without escaping it.
I could reproduce your error with the following code:

empty_var=
if [ $empty_var  = "value" ]; then echo condition ok; fi

which gives me:

bash: [: =: unary operator expected

If we just put $empty_var between double quotes “$empty_var”, then it works:

if [ "$empty_var"  = "value" ]; then echo Condition ok; fi

This will not give an error.

Hi
i am having the same problem using the copy paste of the code from the course:
#!/bin/bash

ARG1=$1

if [ $ARG1 == 'forward' ]; then
    rostopic pub /cmd_vel geometry_msgs/Twist "linear:
  x: -0.1
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0"

elif [ $ARG1 == 'rotate' ]; then
    rostopic pub /cmd_vel geometry_msgs/Twist "linear:
  x: 0.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.2"

elif [ $ARG1 == 'stop' ]; then
    rostopic pub /cmd_vel geometry_msgs/Twist "linear:
  x: 0.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0"
fi

i get the folowing:
./demo.sh: line 5: [: ==: unary operator expected
./demo.sh: line 15: [: ==: unary operator expected
./demo.sh: line 25: [: ==: unary operator expected

i was having the same isue in the exam took me a while to trial and error what was wrong i think it has something to do with spaces? but since it is in a course a copy paste should work?

Hi @ziga.rupret,

if it is a copy-paste and didn’t work, then it is probably because you didn’t pass any param to the script.
You have to call it like:

./my_script.sh rotate

Instead of only

./my_script.sh

yeah that was it, Jesus how could i missed that

1 Like