in , ,

LinuxLinux

How To Check Two Strings Are Equal Using Bash Shell Script

Check Two Strings are equal or not

B

ash Shell Script Example: The Bash is the command line language & Unix Shell. The Linux, Unix & Mac OS adopt this technology and made their default shell as Bash Shell Scripting. The Shell scripting can be used for many purposes and it is the most powerful tool. You can do the automatic task, disk cleanup & data backup using this tool.

Jump To:

Operators used to check two strings are equal or not:

You can use the conditional operator to check the two strings are equal or not. Along with the Bash If Statement you need to use the == to check the two strings is equal.

You can use the != along with Bash If Statement to check the two strings are not equal.

Note: You must need to add the single space before & after the operators == & !=

Basic Example for checking two strings:

Below we provide the example bash shell script to check the two strings are equal!

#!/bin/bash
 
string1="Hello World"
string2="Hello World"
 
if [ "$string1" == "$string2" ]; then
    echo "Given Strings are equal"
else
    echo "Given Strings are not equal"
fi

The output of the above code is,

Output:
Given Strings are equal

Now the example to check the given two strings are not equal using operators.

#!/bin/bash
 
string1="Hello World"
string2="Hello Linux"
 
if [ "$string1" == "$string2" ]; then
    echo "Given Strings are equal"
else
    echo "Given Strings are not equal"
fi

The output of the above code is,

Output:
Given Strings are not equal

The output shows that the above-given strings are not equal.

Check this too  Add Two Numbers in Shell Script Bash Linux!

Verdict:

That’s it, Friends. We hope that you just learned that how to check two given strings are equal or not! If you have any queries and then feel free to comment us below.

  • Operators Used
  • Example Used
5.0

Summary

For more article, Kindly visit our HomePage

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@] gmail.com
Sub: Message to Julian

LibreOffice Ubuntu

LibreOffice Ubuntu: How To Install LibreOffice for Ubuntu 18.04 LTS and 19.04

ElectronMail Ubuntu

ElectronMail: Install ElectronMail for Ubuntu 18.04 LTS and 19.04 & More