The following code always ends up in the else part - also if I enter "peter" or "root". I do not get a syntax error. What am I doing wrong?

# Hello World bash script
echo "What is your name?"
READ answer
if [ "$answer" == "root" ] 
        echo "Hello Administrator"
    elif [ "$answer" == "peter" ] 
            echo "Hello Peter"
        echo "Hello User"
  • Standard troubleshooting recommendation: put set -x before the problem section, and the shell will print what it's executing and you can see what's different from what you expected. (Though it can be confusing, because it prints something equivalent to what it's executing, and it sometimes picks an unexpected equivalent form for commands.) BTW, inside [ ], you should use a single = for comparison; bash allows ==, but it's not standard. – Gordon Davisson Oct 13 '19 at 19:05

The problem is READ answer. Most likely you wanted read answer, which works as expected. You seem to have set up an alias called READ, that does soemthing different.

  • Thank you! This solved the problem :) – Janothan Oct 14 '19 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.