FUNDAMENTALS A Complete Guide for Beginners
In Bash, checking strings for certain conditions is a fundamental aspect of writing a robust script. It’s a common task of string manipulation which is a powerful tool for scripting and automations. In this article, I’ll explore 5 cases to check strings on various constraints like checking empty strings, equality check, finding a substring in a string, and more.
Now, let’s dive into these 5 cases to check string in Bash:
1. Check Empty string
In bash, it’s common to check for an empty string which refers to a string with zero length. Certainly, Bash offers various operators to check the status of strings such as the -z
or -n
option and the equality ==
, or inequality !=
operators.
For example, to check if a string is empty or not, you can use the -z
option within the if-else condition. Follow the script below to check if a string is empty practically:
#!/bin/bash
# Define an empty string
string=""
# Check if the variable is empty using -z
if [ -z "$string" ]; then
echo "The string is empty"
else
echo "The string is not empty"
fi
-z
option in the condition [ -z "$string" ]
checks if the length of the variable $string is zero. Based on the returned value, the echo command displays whether the string is empty or not.The output shows that “The string is empty” as the length of the string variable is zero.
2. Check the Equality of Strings
String equality refers to comparing two strings and checking if they are equal or not. It can be checked by using the single equal =
, double equal ==
operator, or test command within the if-else statement.
For example, to check if two strings are equal or not, follow the script below:
#!/bin/bash
#Define two string variables
string1="Linuxsimply"
string2="Linuxsimply"
#Print the declared strings
echo "Frist string: $string1"
echo "Second String: $string2"
#check two strings equality
if [[ "$string1" == "$string2" ]]; then
echo "strings are equal"
else
echo "strings are different"
fi
"$string1" == "$string2"
within the if-else statement compares string1 and string2 and checks their equality with the double equal ==
operator.The output shows that the strings are equal to each other.
3. Check If the String Contains a Substring
Checking if a string contains another string or substring is called partial comparison. You can use the wildcard character *
, the “grep” command, or even use the “case” statement to check if the string contains a specific substring.
Check the following script to check if a string contains the substring using wildcard character (*) and double equal (‘==’) operator:
#!/bin/bash
#Define a string and another variable with the substring to check.
string="Check if the string contains a substring."
substring="substring"
#Print the declared strings
echo "Frist string: $string1"
echo "Second String: $string2"
#checking the substring into the string
if [[ "$string" == *"$substring"* ]]; then
echo "Substring found: $substring"
else
echo "Substring not found: $substring"
fi
[[ "$string" == *"$substring"* ]]
statement checks if the main string contains a specified substring. The ==
operator is used for string comparison and the wildcard *
is used to match the substring considering any characters before and after the substring.After searching for the substring in the string, the output shows “substring found”.
4. Check If the String Starts with a Specific Value
You can check whether a string starts with a specific value or not using the wildcard (*) character after the specific value. To check if a string starts with a specific value, you can check the following script:
#!/bin/bash
# Define the first string
string="LinuxSimply"
#Check if the string starts with some value
if [[ $string = L* ]]
then
echo "Start with L"
else
echo "No match"
fi
[[ $input = L* ]]
checks if the value of the string begins with L
. If the condition is true, the following block of code is executed.The output shows that “The string starts with L” after checking whether the string starts with the specified value “L”.
5. Check the Lexicographical Order of String
Lexicographical comparison refers to comparing a string based on alphabetical order or dictionary order. The lexicographical comparison usually relies on the ASCII or Unicode values of the characters. In the ASCII system, each character is assigned a numerical value. Lexicographical comparison is executed based on these numerical values. Characters with lower numerical values are positioned before characters with higher numerical values.
Follow the below script to check the lexicographical order of strings:
#!/bin/bash
# Define the first string
stringA="Orange"
# Define the second string
stringB="Apple"
#Print the declared strings
echo "Frist string: $stringA"
echo "Second String: $stringB"
# Check lexicographical order
if [[ "$stringA" < "$stringB" ]]; then
echo "Lexicographical order: $stringA $stringB"
else
echo "Lexicographical order: $stringB $stringA"
fi
[[ "$stringA" < "$stringB" ]]
condition of if block checks if stringA is less than stringB or not. The else block checks the opposite.
The output shows the sorted order of stringA and stringB according to the lexicographical order.
Conclusion
To sum up, this article discusses 5 cases of checking strings in Bash, encompassing checks for empty strings, equality, lexicographical order, and more. Hope this article guides you and clears your vision on Bash check strings to smooth the ways of string manipulation.
People Also Ask
How to compare string lengths in Bash?
To compare the length of strings for your convenience, use the equal -eq
operator. See the below script for a practical approach:
#!/bin/bash
#define two strings
string1="LinuxSimply"
string2="Linux"
#Compare the string lengths
if [ ${#string1} -eq ${#string2} ]; then
echo "Strings are of equal length."
else
echo "Strings are of different lengths."
fi
## Output
Strings are of different lengths.
How to use “regex” in Bash for searching within strings?
Regular expression (regex) is a sequence of characters that defines a search pattern. It provides a tool for pattern matching and searching within strings. Regular expressions are used in various programming languages, text editors, and command-line tools for tasks such as searching, matching, and manipulating text based on specific patterns.
To use regex expression, you can use the [[
keyword along with the =~
operator to perform expression matching. Here’s a basic example to check if a string contains any numbers:
#!/bin/bash
#Declare the string
string="Hello, world!"
# Check if the string matches a basic pattern
if [[ $string =~ [0-9]+ ]]; then
echo "String contains a number."
else
echo "No numbers found in the string."
fi
##Output
No numbers found in the string.
How to check if the string contains only numeric characters or not?
To check whether the string contains only numeric characters or not, you can use regex =~
within the if-else statement. To check if the string contains numeric characters only, follow the below script:
#!/bin/bash
#define the string
string="123"
#check if the string contains only numerical characters
if [[ "$string" =~ ^[0-9]+$ ]]; then
echo "String contains only numbers."
else
echo "String contains non-numeric characters."
fi
##Output
String contains only numbers.
How to check if a string is empty using the “-n” option?
To check if a string is empty, use the -n
option within the if-else statement that checks string length using [ -n “$string” ]
syntax. If the string length is not zero, it evaluates true, otherwise, it evaluates false and the “else” block will find the empty string. To check if a string is empty or not, follow the below script:
#!/bin/bash
#Declare strings
string=""
#Check if the string is empty using "-n" option
if [ -n "$string" ]; then
echo "The string is not empty."
else
echo "The string is empty."
fi
####Output
The string is empty.
How to check if a variable string begins with another string?
You can use the regex operator (=~) to check whether a variable string starts with another string or not. Follow the below script for a better understanding:
#!/bin/bash
#Declare strings
string="Linux Simply"
prefix="Linux"
#check if the string starts with the other string
if [[ "$string" =~ ^"$prefix" ]]; then
echo "The string starts with the “Linux”."
else
echo "The string does not start with the “Linux”."
fi
####Output
The string starts with the “Linux”.
Related Articles
- Check If Bash String Starts with Some Value [4 Methods]
- Check If A String Equals to Another String in Bash [5 Methods]
<< Go Back to Bash String | Bash Scripting Tutorial