0

I'm trying to learn basics of shell scripting when I run the following code my terminal return "None of the condition met" but when I run it on an online shell I get "a is equal to b" can anyone explain the reason for this.

Source:

#!/bin/sh

a=10
b=10

if [ $a == $b ]
then
   echo "a is equal to b"
else
   echo "None of the condition met"
fi

Screenshot:
enter image description here

tripleee
  • 175,061
  • 34
  • 275
  • 318
Shashank K
  • 388
  • 4
  • 13

3 Answers3

0

Ubuntu /bin/sh is dash. You could do something like,

#!/bin/sh

a=10
b=10

if [ "$a" -eq "$b" ]
then
   echo "a is equal to b"
else
   echo "None of the condition met"
fi
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

The == operator is not portable, and apparently the shell you have installed as /bin/sh is using a builtin version of [ that does not recognize that operator. Just replace it with =.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

sh(1) in ubuntu is a symbol link to dash(1):

$ which sh | xargs ls -l
lrwxrwxrwx 1 root root 4 Jan 20 2015 /bin/sh -> dash

according to its man page, there's no == operator in test(1) or [. == is an extended operator for bash(1).

There's two ways to change:

  • if you want to compare two integer, use '-eq' instead
  • if you want to compare two string, use '=' instead.

And both ways are posix-compatible.

oxnz
  • 835
  • 6
  • 16