0

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?

2
  • 2
    try "$(hostname)" == "cps1214" instead Commented Nov 8, 2013 at 20:56
  • 2
    I think you got Bash and Perl confused. Bash -eq is specifically for numeric comparison, not strings. Commented Nov 8, 2013 at 21:06

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

tldp.org/LDP/abs/html/comparison-ops.html Also beware white space and note the number of brackets effect on the comparison.
the double-quotes used in examples I looked at led me to believe they were comparing as strings, but evidently double-quotes get sprayed everywhere in bash scripts. Changing to == has it working on both systems.

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.