Shell script comparison does not work as intended
The following code is supposed to compare two strings or variables. But, I cannot do it. Here, output doesn't depend upon the changing of inputs. Can anyone correct this code?
#!/bin/sh
echo "Enter your username1:"
read username1
echo "Enter your username2:"
read useraname2
if [ "$username1"=="$username2" ]
then
echo 'Same'
else
echo 'Different'
fi
Solution 1:
Quick fix: add spaces around ==
The [
command does different things depending on how many arguments it receives. See http://www.gnu.org/software/bash/manual/bashref.html#index-test
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.