1. Home
  2. Bash
  3. Examples
  4. Check if two strings are equal?

Check if two strings are equal?

Bash – Check If Two Strings are Equal

Brief: This example will help you to understand to check if two strings are equal in a bash script. This shell script accepts two string in variables and checks if they are identical.

Details

  • Use == operator with bash if statement to check if two strings are equal.
  • You can also use != to check if two string are not equal.
  • You must use single space before and after the == and != operators.

Example

In this script two variables are initialized with predefined strings. Now check if both strings are equal or not with == operator.

Output:

Strings are equal

Another Example

Use below sample shell script to take input from the user and check if given strings are equal or not.

Script execution results:

First Run:
StEnter first string: welcome bash
Enter second string: welcome bash
Strings are equal

Second Run:
Enter first string: Welcome bash
Enter second string: hello bash
Strings are not equal

Tags , , ,