Sign up ×

The follwing code is for to compare two strings or variables. But, I cannot do it. I did a lot of googling, but no use. Here, output doesn't depend upon the changing of inputs. Can any one correct this code ??

echo "Enter your username1:"  
read username1  
echo "Enter your username2:"  
read useraname2  
if [ "$username1"=="$username2" ]  
echo 'Same'  
echo 'Different'  
share|improve this question

2 Answers 2

Quick fix: add spaces around ==

The [ command does different things depending on how many arguments it receives. See

When given 1 argument, the test command evaluates to true if the argument is a non-empty string. In your case that is guaranteed to be the case.

share|improve this answer
Should also mention that == is not a standard operator for the [ command. In bash, == is treated the same as =, but for sh, only = is defined to work. –  geirha Aug 9 '13 at 17:44

This ran on my shell:


echo "Enter Username1:"

read username1

echo "Enter Username2:"

read username2



echo "Same"


echo "different"


share|improve this answer
So the basic problem is, as @geirha said, with your == operator use. Please try with = operator; the above script ran on my bash shell, sorry for the !/bin/sh. For sh, the same ran successfully with a single equal (=) sign for obvious reasons. –  Subhadeep Aug 18 '13 at 18:52

Your Answer


