Hello,
I am trying to compare two variables, which both have a word stored in it.
The code I am using is. Please tell me which one of these is correct.Or please tell me the correct syntax.
Thankyou
if [ $variable1=$variable2 ] then
if [ $($variable1)=$($variable2) ] then
if [ "$variable1"="$variable2" ] then
if [ "$variable1" = "$variable2" ] then
if [ "$variable1" -eq "$variable2" ] then
None of them seems to work for me.
if both variables contain string without space
if [ $var1 = $var2 ] ; then
will work
if they have space put variables in double qoutes
Also if either variable my be empty then put variables is double quotes:
$ echo ${FRED}
$ FREDA=Freda
$ echo ${FREDA}
Freda
$ if [ ${FRED} = ${FREDA} ]; then echo Equal; else; echo Not equal; fi
bash: [: =: unary operator expected
Not equal
$ if [ "${FRED}" = "${FREDA}" ]; then echo Equal; else echo Not equal; fi
Not equal
$