1. Home
  2. Bash
  3. Bash Tutorial
  4. Bash – String Comparisons

Bash – String Comparisons

Bash String Comparisons

Use double equals ( == ) operator to compare strings inside square brackets []. Using this option you simply test if two given strings are equals or not inside bash shell scripts.

Example Syntax:

if [ "$str1" == "$str2" ]     # True if equal
if [ "$str1" != "$str2" ]     # True if not equal

Example:

For example to compare two string are equal or not.

if [ "hello" == "hello" ]    ## True
if [ "hello" == "hello1" ]   ## False

if [ "hello" != "hello" ]    ## False
if [ "hello" != "hello1" ]   ## True

Take input of two string in a script and compare if both are same or not, using if statement.

Tags , , ,