11 February 2012

String Comparison Operators in Shell Script

Leave a Comment

Here are Some String Comparison operators used in Shell Script to compare two Strings.
OPERATOR DESCRIPTION
str1 = str2 True if str1 and str2 are identical
str1 != str2 True if str1 and str2 are not identical
-n str1 True if str1 is not null (size is greater than zero)
-z str1 True if str1 is null

Example


$ cat > strtest.sh
#!/bin/bash
a='sandeep'
b='sandeep'
c='vaibhav'
#null string
d=' '

if [ $a = $b ] ; then
echo "a and b are identical"
fi

if [ $a != $c ] ; then
echo "a and c are not identical"
fi

if [ -z $d ] ; then
echo "d is a null string"
fi

Output


That's it.
Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment