Bash string comparison

In this tutorial, How do I use bash string comparison? In bash shell use if statement “==” to check equality and “!=” to check inequality of the string. String comparison examples. Bash script the essential for DevOps Roles.

Bash script string compare use “==” operator

#!/bin/bash
STRA=huu
STRB="www.devopsroles.com"
if [[ "$STRA" == "$STRB" ]]; then
   echo "$STRA equal $STRB"
else
   echo "$STRA not equal $STRB"
fi

The screen output terminal:

Bash string comparison

Bash script string compare use “!=” operator

#!/bin/bash
STRA=huu
STRB="www.devopsroles.com"
if [[ "$STRA" != "$STRB" ]]; then
   echo "$STRA not equal $STRB"
else
   echo "$STRA equal $STRB"
fi

The screen output terminal:

Bash string comparison

Bash script string compare use wildcards

#!/bin/bash
STRA=huu
STRB="www.devopsroles.com"
if [[ "$STRA" == *$STRB* ]]; then
   echo "$STRA equal $STRB"
else
   echo "$STRA not equal $STRB"
fi

The screen output terminal:

Bash string comparison

Conclusion

Thought the article, you can use Bash string comparison as above. I hope will this your helpful. More details refer to Bash script.

,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

Comment moderation is enabled. Your comment may take some time to appear.

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