0

The output I get is sh21.sh: 5: [: xhi: unexpected operator no match

My code is as follows:

#!/bin/bash
s1="hi"
s2="hi"
s3="hello"
if [ "x$s1" == "x$s2" ]
then
  echo match
else
  echo no match
fi

Please explain to me what I am doing wrong.

4
  • 2
    I executed this and it matches. I dont see the problem, may be you are not executing this with bash shell? Commented Sep 14, 2015 at 4:07
  • 1
    There is only ONE = in POSIX test construct. Commented Sep 14, 2015 at 4:26
  • @DavidC.Rankin what is the POSIX test construct? test is a program. Commented Sep 14, 2015 at 4:36
  • 1
    It is a keyword. When you type test or [ in a shell, you are telling the shell to test what follows. (I think [ is actually an alias of test) POSIX shell is just that subset of features that is more or less guaranteed to work with every shell. Things like [[ are referred to as Bashisms because they only work with bash and are not guaranteed to be portable between shells. Oh, and yes I see what you are saying, test is a program along with all the others that make up the shell. When I say construct, I just mean what the shell uses. Commented Sep 14, 2015 at 4:40

3 Answers 3

3

If you are going to use bashisms in your script, it is important to use bash. Your code works fine with bash:

$ bash sh21.sh
match

It fails with dash (which is the sh on debian-like systems):

$ sh sh21.sh
sh21.sh: 5: [: xhi: unexpected operator
no match

== is a bashism, meaning it only works under bash or similar shells. If you want a POSIX compatible script, use =. If not, run the script under bash.

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

Comments

2
if [ "x$s1" == "x$s2" ]

should be

if [ "x$s1" = "x$s2" ]

There is only 1 equal sign when using test or [ in shell programming. Bash allows == with [[, but it should not be used with [. Both test and [ are equivalent and are the POSIX test utility. Bash has the [[ operator that is not the same. There are subtle differences in syntax, quoting requirements and available operators between them.

4 Comments

The shebang line says /bin/bash and Bash actually tolerates == with [ as well. I'm guessing the OP incorrectly runs the script with sh scriptname., though.
Yes I saw that, and you're right, but then I thought it better to start out with the rule rather than muddying the water with all the exceptions that bash allows. I don't think I can type that long...
I wouldn't use the word "construct". There is absolutely no special handling with either test or [ that doesn't apply to any other program. (In fact, there is no requirement for test or [ to even be a built-in command; both probably exist in your file system.)
Bad choice of words. utility -- fixed.
0

Maybe you are using Debian based distros, and the default shell is dash, not bash

Check your shell

ls -l /bin/sh /bin/bash

Run the script with bash

bash sh21.sh

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.