String comparison in shell script

The following program shows the comparison of two strings in shell script

#!/bin/sh
str1="happy"

str2="happy"

[ $str1 = $str2 ]

echo $?

The output of the program looks like this

0

Amazed! The metacharacter $? tests the success and failure of the string comparison. The echo statement displays 0 for success and 1 for failure.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s