- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bash String Comparison
When it comes to programming in Bash, string comparison is a fundamental concept that every developer needs to be familiar with. Bash string comparison involves comparing two strings and evaluating whether they are equal, not equal, greater than, or less than each other. Understanding how to compare strings in Bash is essential for writing reliable and robust scripts.
In this article, we'll explore basics of Bash string comparison and provide examples to help you understand concept better. We'll start with an introduction to comparison operators and then move on to different types of string comparison techniques.
Comparison Operators
Bash provides various operators to compare strings, including ==, !=, <, >, -z, and -n. Let's take a closer look at each of these operators.
= = Operator
The == operator checks if two strings are equal. Here's an example −
Example
string1="Hello" string2="Hello" if [ "$string1" == "$string2" ] then echo "The two strings are equal" fi
Output
The two strings are equal
In this example, we have two strings "Hello" stored in variables string1 and string2. == operator checks if two strings are equal, and if they are, program prints "The two strings are equal" on screen.
!= Operator
The != operator checks if two strings are not equal. Here's an example −
Example
string1="Hello" string2="World" if [ "$string1" != "$string2" ] then echo "The two strings are not equal" fi
Output
The two strings are equal
In this example, we have two strings "Hello" and "World" stored in variables string1 and string2. != operator checks if two strings are not equal, and if they are not, program prints "The two strings are not equal" on screen.
< Operator
The < operator checks if first string is less than second string. Here's an example −
string1="Apple" string2="Banana" if [ "$string1" < "$string2" ] then echo "The first string is less than second string" fi
In this example, we have two strings "Apple" and "Banana" stored in variables string1 and string2. < operator checks if first string "Apple" is less than second string "Banana", and if it is, program prints "The first string is less than second string" on screen.
> Operator
The > operator checks if first string is greater than second string. Here's an example −
string1="Banana" string2="Apple" if [ "$string1" > "$string2" ] then echo "The first string is greater than second string" fi
In this example, we have two strings "Banana" and "Apple" stored in variables string1 and string2. > operator checks if first string "Banana" is greater than second string "Apple", and if it is, program prints "The first string is greater than second string" on screen.
-z Operator
The -z operator checks if string is empty. Here's an example −
Example
string1="" if [ -z "$string1" ] then echo "The string is empty" fi
Output
The string is empty
In this example, we have an empty string stored in variable string1. -z operator checks if string is empty, and if it is, program prints "The string is empty" on screen.
-n Operator
The -n operator checks if string is not empty. Here's an example −
Example
string1="Hello" if [ -n "$string1" ] then echo "The string is not empty" fi
Output
The string is not empty
In this example, we have a non-empty string "Hello" stored in variable string1. -n operator checks if string is not empty, and if it is not, program prints "The string is not empty" on screen.
String Comparison Techniques
Apart from comparison operators, there are several techniques for comparing strings in Bash. Let's explore a few of these techniques.
Numeric Comparison
Numeric comparison is used when comparing numbers stored as strings. For example −
Example
string1="10" string2="20" if [ "$string1" -lt "$string2" ] then echo "The first number is less than second number" fi
Output
The first number is less than second number
In this example, we have two numbers "10" and "20" stored in variables string1 and string2. We use -lt operator to check if first number is less than second number, and if it is, program prints "The first number is less than second number" on screen.
Case-Insensitive Comparison
Case-insensitive comparison is used when comparing strings regardless of their case. For example −
Example
string1="hello" string2="HELLO" if [ "${string1,,}" == "${string2,,}" ] then echo "The two strings are equal" fi
Output
The two strings are equal
In this example, we have two strings "hello" and "HELLO" stored in variables string1 and string2. We use ,, parameter expansion to convert both strings to lowercase and then use == operator to check if two strings are equal. If they are, program prints "The two strings are equal" on screen.
Substring Comparison
Substring comparison is used when comparing a part of a string. For example −
Example
string1="Hello World" substring="Hello" if [[ "$string1" == *"$substring"* ]] then echo "The substring is present in string" fi
Output
The substring is present in string
In this example, we have a string "Hello World" stored in variable string1, and a substring "Hello" stored in variable substring. We use == operator with * wildcard to check if substring is present in string, and if it is, program prints "The substring is present in string" on screen.
Additional Techniques
Apart from techniques mentioned above, there are a few more techniques for string comparison in Bash that you may find useful.
Regular Expression Comparison
Regular expression comparison is used when you need to compare strings based on a pattern. For example −
Example
string1="hello world" pattern="^hello.*$" if [[ "$string1" =~ $pattern ]] then echo "The string matches pattern" fi
Output
The string matches pattern
In this example, we have a string "hello world" stored in variable string1, and a regular expression pattern "^hello.*$" stored in variable pattern. We use =~ operator to check if string matches pattern, and if it does, program prints "The string matches pattern" on screen.
Length Comparison
Length comparison is used when you need to compare strings based on their length. For example −
Example
string1="hello" string2="world" if [ "${#string1}" -lt "${#string2}" ] then echo "The first string is shorter than second string" fi
In this example, we have two strings "hello" and "world" stored in variables string1 and string2. We use ${#string} parameter expansion to get length of each string and then use -lt operator to check if first string is shorter than second string. If it is, program prints "The first string is shorter than second string" on screen.
Null Comparison
Null comparison is used when you need to compare if a variable is set or not. For example −
string1="" if [ -z "${string1+x}" ] then echo "The variable is not set" fi
In this example, we have an empty string stored in variable string1. We use ${string+x} parameter expansion to check if variable is set or not and then use -z operator to check if it is not set. If it is not set, program prints "The variable is not set" on screen.
Best Practices for String Comparison in Bash
When working with string comparison in Bash, there are some best practices that you can follow to ensure that your scripts are reliable and efficient. Here are some of best practices that you can consider −
Use Double Quotes
When comparing strings in Bash, it's essential to enclose variables in double quotes to ensure that strings are properly interpreted. For example −
string1="hello" string2="world" if [ "$string1" == "$string2" ] then echo "The two strings are equal" fi
In this example, we use double quotes to enclose variables string1 and string2, ensuring that strings are properly interpreted.
Use [[ ... ]] Instead of [ ... ]
When performing string comparison, it's recommended to use [[ ... ]] conditional expression instead of [ ... ] test command. [[ ... ]] expression provides additional functionality, such as pattern matching and regular expressions. For example −
Example
string1="hello world" pattern="^hello.*$" if [[ "$string1" =~ $pattern ]] then echo "The string matches pattern" fi
Output
The string matches pattern
In this example, we use [[ ... ]] conditional expression to compare string with regular expression pattern.
Conclusion
In conclusion, Bash string comparison is a fundamental concept that every developer should be familiar with. By understanding various comparison operators and techniques, you can write reliable and robust scripts that can handle string comparison effectively. As you continue to work with Bash, you'll encounter different scenarios that require string comparison, and it's essential to be creative and find best approach to handle each scenario. With knowledge you've gained from this article, you're now equipped to handle most string comparison scenarios in Bash.