When writing scripts in just about any language, you’d often need to compare two or more strings to check if they’re equal or not. In programming terms, two strings are considered equal when they have the same length and same sequence of characters.
In this article, we’re going over all the string comparisons you might have to do when churning out Bash scripts.
Also read: How to install Steam on Linux?
Comparison operators compare two values in boolean — the answer is always either true or false. You can use the following operators to compare strings in Bash.
string1 = string2 or string1 == string2: The equality operator returns true if both the operands are the same.
Use the = operator with the test [ command.
Use the == operator with the [[ command for pattern matching.
string1 != string2: The inequality operator returns true if the given operands are not equal, in this case, the given two strings.
string1 =~ regex: The regex operator will return true if string1 matches the regular expression on the right.
string1 > string2: The greater than operator will return true if the operator on the left is greater than the one on the right. In this case, it’ll return true if string1 is greater than string2.
string1 < string2: This operator works in the exact opposite way from the previous operator. The operator will return true if string1 is less than string2.
-z string – Returns true if the string length is zero.
-n string – Returns true if the string length is non-zero.
However, when using these operators, be sure to keep the following things in mind.
Always use a blank space between the binary operator and the operands, in this case, the strings.
Use double quotes around the variable names to avoid any word spilling or globbing.
Since Bash doesn’t segregate variables by “type”, a given variable can be treated as a string or an integer depending on the context.
Also read: 25 essential Linux Terminal commands
To check if two strings are equal or not, you’ll have to use the if command along with the = operator.
#!/bin/bash
VAR1="String1"
VAR2="String2"
if [ "$VAR1" = "$VAR2" ]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
The above snippet will output Strings are not equal as VAR1 and VAR2 contact different strings.
If you want, you can even have the user input the strings. Except for this time, you’re going to have to use the == operator and the [[ command.
#!/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
Lastly, you can also skip the if statement and use the logical and (&&) operator to compare the twos strings
[[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"
Also read: How to change MAC address on Windows and Linux?
Checking for a substring inside of a string requires the use of the wildcard operator. Here’s how.
#!/bin/bash
VAR='Is the word in this sentence?'
if [[ $VAR == *"word"* ]]; then
echo "Yes it is."
fi
Alternatively, you can also use the regex (=~) operator.
#!/bin/bash
VAR='Is the word in this sentence?'
if [[ $VAR =~ .*word.* ]]; then
echo "Yes it is."
fi
If you’re looking to check if a string is empty, try using the -n and -z operators.
#!/bin/bash
VAR=''
if [[ -z $VAR ]]; then
echo "String is empty."
fi
This program will output String is empty as VAR1 is quite literally an empty string.
Also read: How to install Kali Linux on Virtualbox?
If test operators aren’t quite your thing, you can also use the case statement to compare two strings.
#!/bin/bash
VAR="String1"
case $VAR in
"String1")
echo -n "Match found"
;;
MainString| MainString2)
echo -n "MainNS"
;;
esac
The above script will output Match found as VAR matches the string in question.
In lexicographic comparisons, two strings are compared alphabetically by comparing the characters in one string sequentially from left to right.
You can use the following script to compare two strings lexicographically.
#!/bin/bash
VAR1="String1"
VAR2="String2"
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
Also read: How to delete Files in Linux?
Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars. You can reach out to Yadullah at [email protected], or follow him on Instagram or Twitter.
Frequently Asked Questions
How do you compare strings in bash?
When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.
What is == in bash?
== is a bash -specific alias for = , which performs a string (lexical) comparison instead of the -eq numeric comparison. (It's backwards from Perl: the word-style operators are numeric, the symbolic ones lexical.)
Can you compare strings with ==?
In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .Фестиваль в Остаре приносит собой множество новых ограниченных по времени предметов, которые можно забрать в обмен на жетоны. Players can earn tokens by participating in all Ostara Festival events. With 1,000 chips needed to buy everything, you'll want to focus your efforts. This guide will tell you how many tokens are awarded for each new event and quest in the festival.
Talk to Norvid at the festival store – 20 coins
Screenshot
One of the first things to do is talk to Norvid. He runs a festival shop where you can buy all the temporary items. He gives you a quest and 20 tokens as a nice gesture. At least it’s a good start.
Holiday Quest – 50 chips.
Screenshot
This is a job Norvid gives you. You must decorate five places around Ravensthorpe and return to them. You will receive 50 tokens as a reward.
Queen of May Quest – 140 rooms
Screenshot
This is the most fun of all the quests in Ostara. You have to hunt and fight, face to face with a vicious enemy like you’ve never seen before. It’s worth telling the story, just like the chips.
Egg hunt hunt – 140 tokens
Screenshot
The egg hunt is another unique event for which you will receive 140 tokens. It may take a while, but it’s worth it in the end. You can spread the search over several days, allowing you to participate in other events in between.
Mental protection – 140 tokens
Screenshot
In the final quest of the Ostara Festival, you will encounter spirits from the other side. But first you have to set off eight fireworks around Ravensthorpe, which can take quite a while. Use a raven to track the position of each fireball to speed up this process.
Once you’ve completed all the quests in the Ostara Festival, it’s time to earn coins through repetitive actions.
Viking battle – 140 pieces
Screenshot
You can earn up to 140 chips by completing the Viking Brawl task. However, it requires you to defy and defeat all enemies. You can cancel the event after successfully defeating all opponents and walk away with your winnings. Given the time involved and the possibility of losing everything, it’s only worth participating for the sake of participating.
Rotating Targets– 40 pieces
Screenshot
This is the activity that will earn you the most coins for the time you spend on it. Each round lasts two minutes and you can earn up to 40 stones per round. Even though you can get 25 or 30 if you’re not fast enough with your shots, it’s still the best way to earn chips if you don’t mind repeating it dozens of times.
Brown’s Folly – 10 chips
Screenshot
This ultimate repetitive activity is only worth it if you like running with an intuitive avore. You get ten chips if you beat Brown in this drinking game, that’s it. It takes a little longer than the Swirling Targets activity, so it takes if you’re in a hurry to get chips.
Discover new game consoles, hip new games, great deals, flash offers and more.
frequently asked questions
Can you get married in Assassin’s Creed Valhalla?
With the announcement of the latest Assassin’s Creed installment in Valhalla, it looks like we’re going to get the biggest game ever released by Ubisoft. … Players can once again hunt for a plethora of romantic options in the game, with the possibility of attending Viking weddings being a very real option.
Ezio in Assassin’s Creed Valhalla?
Play as Ezio in Assassin’s Creed Valhalla (AC Valhalla Ezio Legacy Outfit & Legendary Character)
Does AC Valhalla have a choice?
On your trip to AC Valhalla, you’ll be spoilt for choice. … However, there are more than a few actions or dialogue options that can change the ending you get when you finally cross the finish line of Assassin’s Creed Valhalla.
Related Tags: