user1508893
user1508893

Reputation: 9783

Comparing strings in bash script

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

Upvotes: 3

Views: 5594

Answers (2)

nneonneo
nneonneo

Reputation: 179392

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" ]
$

Upvotes: 6

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

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.

Upvotes: 3

Related Questions