In Bash scripting, comparing strings is a common task, especially when dealing with conditional logic and decision-making. Bash provides several ways to compare strings, allowing you to determine equality, inequality, and other relationships between string values.
Bash offers several operators for comparing strings:
1. Equality (==
)
2. Inequality (!=
)
3. Greater than (>
)
4. Less than (<
)
These operators are typically used within conditional statements like if
or while
.
To check if two strings are equal, use the ==
operator within an if
statement.
1. Basic Equality Check:
string1="hello"
string2="hello"
if [ "$string1" == "$string2" ]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
Output:
Strings are equal
string1
is equal to string2
.
2. Inequality Check:
Use the !=
operator to check if strings are not equal.
string1="hello"
string2="world"
if [ "$string1" != "$string2" ]; then
echo "Strings are not equal"
else
echo "Strings are equal"
fi
Output:
Strings are not equal
Bash allows you to compare strings lexicographically (alphabetically) using >
and <
operators.
3. Greater Than Comparison:
string1="apple"
string2="banana"
if [[ "$string1" > "$string2" ]]; then
echo "$string1 is greater than $string2"
else
echo "$string1 is not greater than $string2"
fi
Output:
apple is not greater than banana
4. Less Than Comparison:
string1="apple"
string2="banana"
if [[ "$string1" < "$string2" ]]; then
echo "$string1 is less than $string2"
else
echo "$string1 is not less than $string2"
fi
Output:
apple is less than banana
test
and [
for String Comparisons
Bash provides the test
command and its shorthand [
for string comparisons, which are commonly used in scripts.
5. Using test
for Equality:
string1="hello"
string2="hello"
if test "$string1" = "$string2"; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
Output:
Strings are equal
6. Using [
for Equality:
string1="hello"
string2="world"
if [ "$string1" = "$string2" ]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
Output:
Strings are not equal
To compare strings without considering case, you can convert both strings to the same case using tr
or ,,
(for lowercase) and ^^
(for uppercase) within Bash.
7. Converting to Lowercase for Comparison:
string1="Hello"
string2="hello"
if [ "${string1,,}" == "${string2,,}" ]; then
echo "Strings are equal (case-insensitive)"
else
echo "Strings are not equal"
fi
Output:
Strings are equal (case-insensitive)
8. Converting to Uppercase for Comparison:
string1="Hello"
string2="HELLO"
if [ "${string1^^}" == "${string2^^}" ]; then
echo "Strings are equal (case-insensitive)"
else
echo "Strings are not equal"
fi
Output:
Strings are equal (case-insensitive)
You might also need to check if a string is empty or not.
9. Check if a String is Empty:
string=""
if [ -z "$string" ]; then
echo "String is empty"
else
echo "String is not empty"
fi
Output:
String is empty
10. Check if a String is Not Empty:
string="hello"
if [ -n "$string" ]; then
echo "String is not empty"
else
echo "String is empty"
fi
Output:
String is not empty
[[...]]
for advanced string comparisons, such as using >
and <
for lexicographic ordering.
Understanding how to compare strings in Bash is crucial for effective scripting, especially in conditional logic. By mastering these comparison techniques, you can build more dynamic and responsive Bash scripts.
Jorge García
Fullstack developer