How to compare strings in Bash

28/12/2020

For different programming purposes, we need to compare the value of two strings. Built-in functions are used in many programming language to test the equality of two strings. You can check the equality and inequality of two strings in bash by using if statement. “==” is used to check equality and “!=” is used to check inequality of the strings. You can partially compare the values of two strings also in bash. How you can compare the string values in bash is shown using various examples in this tutorial.

Example-1: String Comparison using “==” operators

There is no built-in comparison function to check equality of two string values in bash like other standard programming language. In the following script, two string variables, strval1 and strval2 are declared. The equity of these two string variables are tested using the first if statement of the script. The value of strval1 is compared with a string value to check the equality in the second if statement.

#!/bin/bash

strval1="Ubuntu"
strval2="Windows"

#Check equality two string variables

if [ $strval1 == $strval2 ]; then
  echo "Strings are equal"
else
  echo "Strings are not equal"
fi

#Check equality of a variable with a string value

if [ $strval1 == "Ubuntu" ]; then
  echo "Linux operating system"
else
  echo "Windows operating system"
fi

Output:

First comparison is not equal and second comparison is equal.

Example-2: String Comparison using “!=” operator

The inequality of two string variables are checked in the following example. Here two values are not equal. So, if condition will be true and “Windows operating system” will print.

#!/bin/bash

strval1="Ubuntu"
strval2="Windows"

#Check inequality of a variable with a string value

if [ $strval2 != "Ubuntu" ]; then
  echo "Windows operating system"
else
  echo "Linux operating system"
fi

Output:

Example-3: Partial String Comparison

You can compare partial value by using wild card character in bash script. In the following script, “*” is used as wild card character for partial matching.  The string variable, strval contains the word “Internet”. So, the first if of the script will return true and print “Partially Match”. Bash is case sensitive. For this, the second if of the script will return false for using “internet” as partial string which is  not equal by letter wise comparison.

#!/bin/bash

strval="Microsoft Internet Explorer"

if [[ $strval == *Internet* ]];
then
  echo "Partially Match"
else
  echo "No Match"
fi

if [[ $strval == *internet* ]];
then
  echo "Partially Match"
else
  echo "No Match"
fi

Output:

Example-4: Compare string with user input value

Sometimes, we need to compare the string value taken by the user with specific string value for programming purpose. In the following example, a string data will be taken from user as input and compared the inequality of the data with a fixed value. If the condition is true then it will print “No Record Found”, otherwise it will print “Record Found”.

#!/bin/bash

echo "Enter Your Name"
read input

if [ $input != "Fahmida" ];
then
  echo "No Record Found"
else
  echo "Record Found"
fi

Output:

Video of this lesson is here:

String comparison task in bash will be easier for you after completing the above examples with clear understanding.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash for loop examples

Loops are a very essential part of any type of programming or scripting languages. Like any other standard programming,...
29/12/2020

Bash Alias Tutorial

How to use bash aliases Most of the users like to use shortcuts for running commands. There are many commands in Ubuntu...
28/12/2020

BASH For Loop Examples

Loops are used in any programming language to execute the same code repeatedly. Three types of loops are mainly used in...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024