How to compare strings in bash

Bash string comparison

Bash is a powerful command-line interface that allows us to control our computer by typing commands into a terminal. With Bash, we can navigate the file system, run programs, manage processes, and perform many other tasks. In addition to its interactive use at the command prompt, Bash can also write scripts that automate tasks. Bash scripts are text files that contain a sequence of commands and are usually used to automate tasks that would be repetitive or tedious to perform manually at the command prompt.

We can use the = operator in Bash to see if two strings are equal. For example:

if [ "$string1" = "$string2" ]; then
  echo "Strings are equal"
else
  echo "Strings are not equal"
fi

We can also use the != operator to test if two strings are not equal:

if [ "$string1" != "$string2" ]; then
  echo "Strings are not equal"
else
  echo "Strings are equal"
fi

However, = and != operators perform a case-sensitive comparison. To perform a case-insensitive comparison, we can use the =* and !=* operators instead:

if [[ "$string1" = *"$string2"* ]]; then
  echo "Strings are equal (case-insensitive)"
else
  echo "Strings are not equal (case-insensitive)"
fi

We can also use the == and != operators with the [[ command to perform pattern matching. For example:

if [[ "$string1" == "$string2" ]]; then
  echo "Strings match"
else
  echo "Strings do not match"
fi

For more advanced string comparison operations, we can use the bash builtin [[ command, which supports a variety of string comparison and pattern-matching options.

Below are some examples of advanced string comparison operations that can be performed with the bash builtin [[ command:

  • [[ "$string" == *"substring"* ]]: Test if $string contains "substring" (case-sensitive).

  • [[ "$string" == "$substring"* ]]: Test if $string starts with "substring" (case-sensitive).

  • [[ "$string" == *"substring" ]]: Test if $string ends with "substring" (case-sensitive).

  • [[ "$string" =~ ^[0-9]+$ ]]: Test if $string consists only of digits.

  • [[ "$string" =~ ^[a-zA-Z]+$ ]]: Test if $string consists only of letters.

  • [[ "$string" =~ ^[a-zA-Z0-9]+$ ]]: test if $string consists only of letters and digits.

Here's an example of how these advanced string comparisons could be used:

string="Hello, world!"
if [[ "$string" == *"Hello"* ]]; then
echo "String contains 'Hello'"
fi
if [[ "$string" == "Hello"* ]]; then
echo "String starts with 'Hello'"
fi
if [[ "$string" == *"world!" ]]; then
echo "String ends with 'world!'"
fi
if [[ "$string" =~ ^[0-9]+$ ]]; then
echo "String consists only of digits"
else
echo "String does not consist only of digits"
fi
if [[ "$string" =~ ^[a-zA-Z]+$ ]]; then
echo "String consists only of letters"
else
echo "String does not consist only of letters"
fi
if [[ "$string" =~ ^[a-zA-Z0-9]+$ ]]; then
echo "String consists only of letters and digits"
else
echo "String does not consist only of letters and digits"
fi

Note: For more information about using [[ for string comparisons and pattern matching, we can consult the Bash documentation.

Free Resources