Brad Mace
Brad Mace

Reputation: 27886

"Invalid arithmetic operator" error when checking hostname in Bash script

I have a simple hostname check in a bash script:

if [[ `hostname` -eq "cps1214" ]]
then
    JAVA_HOME=/usr/local/jdk1.6.0_21
fi

On our old SuSE 8 system this works fine. On our newer CentOS system this causes an error:

[[: dev.example.com: syntax error: invalid arithmetic operator
(error token is ".example.com")

I'm not really sure what the issue is here. My understanding is that -eq is explicitly for string comparisons, hostname is clearly returning a string, and the right-hand side is also a string. Why is it complaining about arithmetic?

Upvotes: 0

Views: 1873

Answers (1)

user2719058
user2719058

Reputation: 2233

Because -eq is not for string comparisons, but an arithmetic operator, as described in the bash documentation.

You can swap -eq with = or ==, and you are fine.

Upvotes: 3

Related Questions