BASH SCRIPT – In this tutorial, you will understand how to use strings in shell script, string operations in bash, bash string comparison, bash string comparison not equal, bash string equals, bash string greater than, bash string less than with example programs.
- In shell, the strings are defined using single quote or double quotes
- It is important to note that, the equal (=) is used to compare the two strings in the shell.
STRING OPERATORS
S.N | OPERATORS | DESCRIPTION |
1. | = | It is used to check whether the two strings are equal |
2. | != | It is used to check whether the two strings are not equal |
3. | \> | It is used to check whether the first string is greater than second string |
4. | \< | It is used to check whether the first string less than second string |
5. | -n | This option is used to check whether the string is not empty |
6. | -z | This option is used to check whether the string is empty |
I. STRING COMPARISON USING EQUAL OPERATOR
1. SOURCE CODE
echo “————————————-“
echo “\t String Comparison”
echo “————————————-“
echo “Enter the name 1: “
read s1
echo “Enter the name 2: “
read s2
# compare the strings using EQUAL operator
if [ $s1 = $s2 ]
then
echo “Both Strings are equal”
else
echo “Both Strings are NOT equal”
fi
2. OUTPUT
II. STRING COMPARISON USING NOT EQUAL OPERATOR
1. SOURCE CODE
echo “————————————-“
echo “\t String Comparison using !=”
echo “————————————-“
echo “Enter the name 1: “
read s1
echo “Enter the name 2: “
read s2
# compare the strings using NOT EQUAL operator
if [ $s1 != $s2 ]
then
echo “Both Strings are NOT equal”
else
echo “Both Strings are equal”
fi
2. OUTPUT
III. BIGGEST OF TWO STRINGS USING \> OPERATOR
1. SOURCE CODE
echo “————————————-“
echo “\t Biggest of Two Strings \>”
echo “————————————-“
echo “Enter the name 1: “
read s1
echo “Enter the name 2: “
read s2
# compare the strings using > operator
if [ $s1 \> $s2 ]
then
echo “$s1 is GREATER than $s2”
else
echo “$s2 is GREATER than $s1”
fi
2. OUTPUT
IV. SMALLEST OF TWO STRINGS USING \< OPERATOR
1. SOURCE CODE
echo “————————————-“
echo “\t Smallest of Two Strings \<“
echo “————————————-“
echo “Enter the name 1: “
read s1
echo “Enter the name 2: “
read s2
# compare the strings using less than operator
if [ $s1 \< $s2 ]
then
echo “$s1 is LESSER than $s2”
else
echo “$s2 is LESSER than $s1”
fi
2. OUTPUT
V. EXAMPLE OF STRING NOT EMPTY
1. SOURCE CODE
echo “————————————-“
echo “\t String Not Empty”
echo “————————————-“
str=”Shiva”
# check string is not empty
if [ -n $str ]
then
echo “String is NOT Empty”
echo “Name: $str”
else
echo “String is Empty”
fi
2. OUTPUT
VI. EXAMPLE OF STRING EMPTY
1. SOURCE CODE
echo “————————————-“
echo “\t String Empty”
echo “————————————-“
str=””
# check string is empty
if [ -z $str ]
then
echo “String is Empty”
else
echo “String is Not Empty”
fi
2. OUTPUT
More Tutorials
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…