How to Compare Strings in Bash
When composing Bash contents you will regularly need to contrast two strings with check in the event that they are equivalent or not. Two strings are equivalent when they have a similar length and contain similar succession of characters.
This instructional exercise depicts how to analyze strings in Bash.
Comparison Operators
Examination administrators are administrators that analyze qualities and return valid or bogus. When looking at strings in Bash you can utilize the accompanying administrators:
- string1 = string2 and string1 == string2 - The equity administrator returns valid if the operands are equivalent.
- Utilize the = administrator with the test [ order.
- Utilize the == administrator with the [[ order for design coordinating.
- string1 != string2 - The imbalance administrator returns valid if the operands are not equivalent.
- string1 =~ regex-The regex administrator returns valid if the left operand coordinates the all-encompassing standard articulation on the right.
- string1 > string2 - The more prominent than administrator returns valid if the left operand is more noteworthy than the privilege arranged by lexicographical (sequential) request.
- string1 < string2 - The not as much as administrator returns valid if the correct operand is more noteworthy than the privilege arranged by lexicographical (sequential) request.
- - z string - True if the string length is zero.
- - n string - True if the string length is non-zero.
Following are a couple of focuses to be noted when looking at strings:
A clear space must be utilized between the parallel administrator and the operands.
Continuously utilize twofold statements around the variable names to evade any word parting or globbing issues.
Slam doesn't isolate factors by "type", factors are treated as number or string contingent upon the specific circumstance.
Check if Two Strings are Equal
As a rule, when contrasting strings you would need with check if the strings are equivalent.
The accompanying content uses the if proclamation and the test [ order to check if the strings are equivalent or not with the = administrator:
#!/bin/bash
VAR1="Linuxize"
VAR2="Linuxize"
if [ "$VAR1" = "$VAR2" ]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
At the point when the content is executed it will print the accompanying yield.
Strings are equal.
Here is another content that takes the contribution from the client and thinks about the given strings. In this model, we will utilize the [[ order and == administrator.
#!/bin/bash
read -p "Enter first string: " VAR1
read -p "Enter second string: " VAR2
if [[ "$VAR1" == "$VAR2" ]]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
Run the content and enter the strings when provoked:
Enter first string: Linuxize
Enter second string: Ubuntu
Strings are not equal.
You can likewise utilize the coherent and && and additionally || to look at strings:
[[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"
Not equal
Check if a String Contains a Substring
There are numerous approaches to check if a string contains a substring.
One methodology is to utilize encompass the substring with indicator images * which means coordinate all characters.
#!/bin/bash
VAR='GNU/Linux is an operating system'
if [[ $VAR == *"Linux"* ]]; then
echo "It's there."
fi
The content will repeat the accompanying:
It's there.
Another choice is to utilize the regex administrator =~ as demonstrated as follows:
#!/bin/bash
VAR='GNU/Linux is an operating system'
if [[ $VAR =~ .*Linux.* ]]; then
echo "It's there."
fi
The time frame followed by an indicator .* matches at least zero events any character aside from a newline character.
Check if a String is Empty
Regularly you will likewise need to check if a variable is a vacant string. You can do this by utilizing the - n and - z administrators.
#!/bin/bash
VAR=''
if [[ -z $VAR ]]; then
echo "String is empty."
fi
String is empty.
#!/bin/bash
VAR='Linuxize'
if [[ -n $VAR ]]; then
echo "String is not empty."
fi
String is not empty.
Comparing Strings with the Case Operator
Rather than utilizing the test administrators you can likewise utilize the case articulation to look at strings:
#!/bin/bash
VAR="Arch Linux"
case $VAR in
"Arch Linux")
echo -n "Linuxize matched"
;;
Fedora | CentOS)
echo -n "Red Hat"
;;
esac
Linuxize matched.
Lexicographic Comparison
Lexicographical examination is an activity where two strings are looked at one after another in order by contrasting the characters in a string successively from left to right. This sort of correlation is seldom utilized.
The accompanying contents think about two strings lexicographically:
#!/bin/bash
VAR1="Linuxize"
VAR2="Ubuntu"
if [[ "$VAR1" > "$VAR2" ]]; then
echo "${VAR1} is lexicographically greater then ${VAR2}."
elif [[ "$VAR1" < "$VAR2" ]]; then
echo "${VAR2} is lexicographically greater than ${VAR1}."
else
echo "Strings are equal"
fi
The script will output the following:
Ubuntu is lexicographically greater than Linuxize.
Conclusion
Contrasting string is one of the most fundamental and often utilized tasks in Bash scripting. In the wake of perusing this instructional exercise, you ought to have a decent comprehension of how to analyze strings in Bash. You can likewise check our guide about string connection .
On the off chance that you have any inquiries or criticism, don't hesitate to leave a remark.