. Advertisement .
..3..
. Advertisement .
..4..
The need for comparing strings in a Bash script is quite common and also can be utilized for specific conditions before the next script part proceeds. With the guidance in this post, you will be shown ways to compare Bash strings and other methods to access strings. Keep on reading if you want to know the details!
Compare Strings in a Bash Script
A string contains any characters sequence. To access whether 2 strings are similar to each other, both of them must include the accurate same characters arranged within the same order. Therefore, string comparison refers to checking whether certain strings are similar to each other or not. The strings are considered the same when they have the equal length as well as include the same characters sequence.
There are diverse operators for comparing strings in a Bash script which results in true/false based on the condition. Some common operators can be mentioned as:
- string1
=
string2: Equality operator with the [ command is used and results in true when there are 2 equal operands. - string1
==
string2: Equality operator with the [[ command is used and results in true when there are 2 equal operands. - string1
!=
string2: Inequality operator will result in true when it indicates inequivalent 2 operands. - string1
=~
regex: Regex operator will result in true when there is a match between string1 and extended regex. - string1
>
string2: The operator of greater than will result in true when string1 is exceeding string2 depending on alphabetical order. - string1
<
string2: The operator of smaller than will result in true when string1 is lower than string2 depending on alphabetical order. -z
string: Result in true when the string length is 0-n
string: Result in true when the string length is not 0
String1="Hello World!!"
String2="Hello World!!"
String3="IT tutoria"
if [ "$String1" = "$String2" ]; then
echo "String1 and String2 are equal."
else
echo "String1 and String2 are not equal."
fi
if [[ "$String1" == "$String2" ]]; then
echo "String1 and String2 are equal."
else
echo "String1 and String2 are not equal."
fi
if [[ "$String1" != "$String3" ]]; then
echo "String1 and String3 are not equal."
else
echo "String1 and String3 are equal."
fi
Output:
String1 and String2 are equal.
String1 and String2 are equal.
String1 and String3 are not equal.
Both string1 and string2 have an equal length and a similar character sequence. Therefore, when you first use the =
operator for strings comparison, the operator will result in true which indicates that String1 =
String2 as the result of the program block if-else
.
Likewise, in the next program, we use the operator ==
to make a String1-String2 comparison. [[ is used for comparing strings in this situation. Finally, the operator !=
is used for comparing String1 versus String3.
Comparing Lexicographic in Bash
Lexicographic comparison is also a common performance in comparing strings in a Bash script. This term refers to how strings are compared depending upon alphabetical order. To conduct a lexicographic comparison, the operators >
and <
are used.
name1="Kamal"
name2="Abinash"
if [[ "$name1" > "$name2" ]]; then
echo "${name1} is greater then ${name2}."
elif [[ "$name1" < "$name2" ]]; then
echo "${name2} is greater than ${name1}."
else
echo "Both the namees are equal"
fi
Output:
Kamal is greater then Abinash.
With this above coding example, you can see there was a lexicographic comparison between name1 versus name2. Since A stands before K in the order of the alphabet, K is greater than A, so “$name1” > “$name2” results in true and indicates that “Kamal is greater then Abinash.” as output.
How to Check for an Empty String
To check for an empty string, the operators -n
and -z
are used.
String=""
if [[ -z $String ]]; then
echo "The variable String is an empty string."
fi
Output:
The variable String is an empty string.
In the above demonstration, String is considered an empty variable. Since -z operator results in true when the string length is 0 so we receive “The variable String is an empty string.” as the program output.
Similarly, String is considered an empty variable. Since -n operator results in true when the string length is 0 so we receive “The variable String is an empty string.” as the program output.
Check Whether a String Includes a Substring
The operator =~(Regex) is used for checking whether a string includes a substring. This operator will result in true when there is a match between the string and the extended expression of regex. To make a comparison, a relevant expression of regex must be created.
String='My name is ITtutoria.'
if [[ $String =~ .*ITtutoria.* ]]; then
echo "The given string has ITtutoria on it."
fi
Output:
The given string has ITtutoria on it.
With the above coding treatment, *ITtutoria.* is the matched expression of regex, which indicated the match in any string before or after ITtutoria, even zero or other characters. It assesses whether the strings contain substring ITtutoria within it.
Since ITtutoria appears in the appointed string, it satisfies the stated condition and we receive “The given string has ITtutoria on it.” as output. This blog has provided you with guidance on comparing strings in a Bash script. You are also introduced to various methods to make string comparisons, check for an empty string, and check whether a string includes a substring. If you expect to figure out more coding examples, visit our website more often!
Leave a comment