String Operators | Shell Script
Pre-Requisite: Conditional Statement in Shell Script
There are many operators in Shell Script some of them are discussed based on string.
Equal operator (=): This operator is used to check whether two strings are equal.
Syntax:
Operands1 = Operand2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="geeks";
if [ $str1 = $str2 ]
then
echo "Both string are same";
else
echo "Both string are not same";
fi
Copy Code
Output:
Both string are not same
Not Equal operator (!=): This operator is used when both operands are not equal.
Syntax:
Operands1 != Operands2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="geeks";
if [ $str1 != $str2 ]
then
echo "Both string are not same";
else
echo "Both string are same";
fi
Copy CodeRun on IDE
Output:
Both string are not same
Less then (\<): It is a conditional operator and used to check operand1 is less then operand2.
Syntax:
Operand1 \< Operand2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="Geeks";
if [ $str1 \< $str2 ]
then
echo "$str1 is less then $str2";
else
echo "$str1 is not less then $str2";
fi
Copy Code
Output:
GeeksforGeeks is not less then Geeks
Greater then (\>): This operator is used to check the operand1 is greater then operand2.
Syntax:
Operand1 \> Operand2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="Geeks";
if [ $str1 \> $str2 ]
then
echo "$str1 is greater then $str2";
else
echo "$str1 is less then $str2";
fi
Copy Code
Output:
GeeksforGeeks is greater then Geeks
Check string length greater then 0: This operator is used to check the string is not empty.
Syntax:
[ -n Operand ]
Example:
#!/bin/sh
str="GeeksforGeeks";
if [ -n $str ]
then
echo "String is not empty";
else
echo "String is empty";
fi
Copy Code
Output:
String is not empty
Check string length equal to 0: This operator is used to check the string is empty.
Syntax:
[ -z Operand ]
Example:
#!/bin/sh
str="";
if [ -z $str ]
then
echo "String is empty";
else
echo "String is not empty";
fi
Copy Code
Output:
String is empty
Pre-Requisite: Conditional Statement in Shell Script
There are many operators in Shell Script some of them are discussed based on string.
Equal operator (=): This operator is used to check whether two strings are equal.
Syntax:
Operands1 = Operand2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="geeks";
if [ $str1 = $str2 ]
then
echo "Both string are same";
else
echo "Both string are not same";
fi
Copy Code
Output:
Both string are not same
Not Equal operator (!=): This operator is used when both operands are not equal.
Syntax:
Operands1 != Operands2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="geeks";
if [ $str1 != $str2 ]
then
echo "Both string are not same";
else
echo "Both string are same";
fi
Copy CodeRun on IDE
Output:
Both string are not same
Less then (\<): It is a conditional operator and used to check operand1 is less then operand2.
Syntax:
Operand1 \< Operand2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="Geeks";
if [ $str1 \< $str2 ]
then
echo "$str1 is less then $str2";
else
echo "$str1 is not less then $str2";
fi
Copy Code
Output:
GeeksforGeeks is not less then Geeks
Greater then (\>): This operator is used to check the operand1 is greater then operand2.
Syntax:
Operand1 \> Operand2
Example:
#!/bin/sh
str1="GeeksforGeeks";
str2="Geeks";
if [ $str1 \> $str2 ]
then
echo "$str1 is greater then $str2";
else
echo "$str1 is less then $str2";
fi
Copy Code
Output:
GeeksforGeeks is greater then Geeks
Check string length greater then 0: This operator is used to check the string is not empty.
Syntax:
[ -n Operand ]
Example:
#!/bin/sh
str="GeeksforGeeks";
if [ -n $str ]
then
echo "String is not empty";
else
echo "String is empty";
fi
Copy Code
Output:
String is not empty
Check string length equal to 0: This operator is used to check the string is empty.
Syntax:
[ -z Operand ]
Example:
#!/bin/sh
str="";
if [ -z $str ]
then
echo "String is empty";
else
echo "String is not empty";
fi
Copy Code
Output:
String is empty
0 Comments
Post a Comment