Open In App

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.

Operands1 = Operand2




#!/bin/sh
 
str1="GeeksforGeeks";
str2="geeks";
if [ $str1 = $str2 ]
then
    echo "Both string are same";
else
    echo "Both string are not same";
fi

Both string are not same
Operands1 != Operands2




#!/bin/sh
 
str1="GeeksforGeeks";
str2="geeks";
if [ $str1 != $str2 ]
then
    echo "Both string are not same";
else
    echo "Both string are same";
fi

Both string are not same




#!/bin/sh
 
str1="GeeksforGeeks";
str2="Geeks";
if [ $str1 \< $str2 ]
then
    echo "$str1 is less than $str2";
else
    echo "$str1 is not less than $str2";
fi

GeeksforGeeks is not less than Geeks




#!/bin/sh
 
str1="GeeksforGeeks";
str2="Geeks";
if [ $str1 \> $str2 ]
then
    echo "$str1 is greater than $str2";
else
    echo "$str1 is less than $str2";
fi

GeeksforGeeks is greater than Geeks
[ -n Operand ]




#!/bin/sh
 
str="GeeksforGeeks";
if [ -n $str ]
then
    echo "String is not empty";
else
    echo "String is empty";
fi

String is not empty
[ -z Operand ]




#!/bin/sh
 
str="";
if [ -z $str ]
then
    echo "String is empty";
else
    echo "String is not empty";
fi

String is empty

Article Tags :