How To

How To Compare Strings In Bash Scripts

Introduction

String comparison is an important part of Bash scripting. In many scripts, you need to compare text values before making a decision. For example, you may want to check if a username is correct, if user input matches a specific word, if a file extension is valid, or if a command output contains an expected value.

In Bash, strings can be compared using conditional statements such as if, else, and elif. This is useful for Linux automation, cybersecurity scripts, log analysis, menu-based scripts, and user input validation.

Basic String Comparison In Bash

The most common way to compare strings in Bash is by using the [[ ]] test expression.

Basic syntax:

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

The == operator checks whether two strings are equal.

Bash Script To Compare Two Strings

Create a new Bash script:

nano compare-strings.sh

Add the following code:

#!/bin/bashname1="kali"name2="kali"if [[ "$name1" == "$name2" ]]; then    echo "Both strings are equal"else    echo "Strings are different"fi

Save the file and run it:

chmod +x compare-strings.sh./compare-strings.sh

Output:

Both strings are equal

Check If Strings Are Not Equal

To check if two strings are not equal, use the != operator.

#!/bin/bashuser="admin"if [[ "$user" != "root" ]]; then    echo "User is not root"else    echo "User is root"fi

This script checks whether the value of user is different from root.

Compare User Input In Bash

String comparison is often used with user input.

#!/bin/bashread -p "Enter username: " usernameif [[ "$username" == "admin" ]]; then    echo "Access granted"else    echo "Access denied"fi

This script asks the user to enter a username and compares it with admin.

Check If A String Is Empty

You can check whether a string is empty using -z.

#!/bin/bashread -p "Enter your name: " nameif [[ -z "$name" ]]; then    echo "Name cannot be empty"else    echo "Hello, $name"fi

The -z operator returns true if the string length is zero.

Check If A String Is Not Empty

To check if a string is not empty, use -n.

#!/bin/bashmessage="Linux Security"if [[ -n "$message" ]]; then    echo "String is not empty"else    echo "String is empty"fi

Case-Sensitive String Comparison

Bash string comparison is case-sensitive by default.

#!/bin/bashword1="Linux"word2="linux"if [[ "$word1" == "$word2" ]]; then    echo "Match found"else    echo "No match found"fi

Output:

No match found

Here, Linux and linux are treated as different strings.

Conclusion

String comparison in Bash is simple and useful for creating smarter scripts. You can compare equal strings using ==, different strings using !=, empty strings using -z, and non-empty strings using -n.

For beginners, learning Bash string comparison is important because it helps scripts make decisions based on text values. This skill is useful in Linux automation, cybersecurity scripting, form validation, log filtering, and command-line tools.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

4 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

4 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

4 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

4 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

4 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

5 days ago