0

I tried

if [ "$str1" == "$str2" && "$str3" == "$str4" ];
then
  statement
else
   statement
fi

I've tried with one equal sign. I've tried with two equal signs. I've tried with and without quotation marks. I've tried without the semi-colon. Nothing seems to work.

I keep getting error

[: missing `]' 

I have not the slightest clue on what that means. I've tried my function with out this if statement and it works. PEOPLE I HAVE A SPACE between the leading and trailing brackets.

Wilson
  • 1
  • 1
  • 1
    I voted to close as a duplicate of the wrong thing - this addresses "and" for two tests http://stackoverflow.com/a/8920266/478656 (but the other suggestion that you need a space before ] still holds). – TessellatingHeckler Apr 29 '16 at 02:57
  • pay attention to the space in the beginning and ending of the [] – huan feng Apr 29 '16 at 03:00
  • what if you have all the spaces between brackets? Is there another reason this might not work? Will nesting if statements work? – Wilson Apr 29 '16 at 03:11
  • @Wilson, keep cool man. For such testing you must use `[[ ... && ... ]]` in your source. Then you can get great debug info by pasting your source in http://www.shellcheck.net/ -- note that you must be sure of the shell you run on. – J. Chomel Apr 29 '16 at 08:56

1 Answers1

0
#!/bin/bash
s1="hi"
s2="hi"

if [ "$s1" == "$s2" ]
then
  echo match
fi

Provide spaces between square brackets and variables and you will get exact output.

Updated for two comparisons:

#!/bin/bash
str1="test"
str2="test"
str3="test"
str4="test"

if [ "$str1" = "$str2" ] && [ "$str3" = "$str4" ]
then
        echo "equals"
fi
exit 0;
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116