3
./build_binaries.sh: line 43: [: ==: unary operator expected

I have this line (line 43) in my bash script which looks correct to me, but it keeps throwing error.

if [ ${platform} == "macosx" ]; then

Error:

./foo.sh: line 43: [: ==: unary operator expected

This is on OSX.

2 Answers 2

6

The problem is that $platform is an empty string. The usual workaround is to put it in quotes:

if [ "${platform}" == "macosx" ]

Example:

$ unset x
$ [ $x == 3 ]
-bash: [: ==: unary operator expected
$ [ "$x" == "3" ]
$
Sign up to request clarification or add additional context in comments.

Comments

3

One possibility is to use a single =. That's the classic notation. Some shells allow ==, but others do not.

Also, you should enclose the ${platform} in double quotes; I think that it is an empty string, and this is confusing things.

platform=
if [  $platform  == mac ]; then echo hi; else echo lo; fi
if [ "$platform" == mac ]; then echo hi; else echo lo; fi

This produces the error you're seeing on the second line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.