How string comparison works in shell script

According to my experience with scripting, shell scripting is completely different than any other scripting languages. While creating one script for database monitoring purpose, got stuck with weird error on string comparison. It looks like very easy but it took so much time for me to found the solution.

While comparing 2 strings in “IF” condition, got stuck with syntax error. After searching lots of things on google finally I have found the solution that we have to keep space before and after the variable or string within the IF bracket
i.e if [“$var”==”string”]

Below conditions will not work while using IF condition in shell script

if [$cluster==”amazon”] ; then
if [“$cluster”==”amazon”] ; then
if [ “$cluster”==”amazon” ] ; then

Below one will work

if [ “$cluster” == “amazon” ] ; then

I hope this will help and save time for others.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.