Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Compare Strings in Bash - DesignLinux

Oct 25 2020
designlinux 0 Comments
How to Compare Strings in Bash

At the time of writing bash scripts, it’s often required to compare two strings to check it wether those are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters. This guide shows you how to compare strings in Bash.

Comparison Operators#

Comparison operators are comparing the values and returns true or false. When comparing strings in Bash you can use the following operators:

  • You can check like string1 = string2 and string1 == string2 and it will returns true if the operands are equal.
    • Use the = operator with the test [ command.
    • Use the == operator with the [[ command for pattern matching.
  • string1 != string2 – It returns true if the operands are not equal.
  • string1 =~ regex – Returns true if the left operand matches the extended regular expression on the right.
  • string1 > string2 – It will return true if the left operand is greater than the right sorted in alphabetical order.
  • string1 < string2 – The less than operator returns true if the right operand is greater than the right sorted by alphabetical order.
  • -z string – True if the string length is zero.
  • -n string – Returns True if the string length is non-zero.

Following points you should keep in mind while comparing strings:

  • There must be a blank space between the binary operator and the operands.
  • Use the double quotes around the variable names to avoid any word splitting issues.
  • Variables in bash are treated as integer or string depending on the context, Bash does not separate variables by “type”.

Check if Two Strings are Equal#

Generally, we need to check that string are equal or not while comparing the strings. Below is an example of script to use the if statement and the test [ command to check if the strings are equal or not with the = operator:

#!/bin/bash

VAR1="TecNStuff"
VAR2="TecNStuff"

if [ "$VAR1" = "$VAR2" ]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

The script will show the following output.

Strings are equal.

In below example, it will take input from the usr and compares the given strings. In this used the [[ command and == operator.

#!/bin/bash

read -p "Enter first string: " VAR1
read -p "Enter second string: " VAR2

if [[ "$VAR1" == "$VAR2" ]]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

Now run the script and you will be asked to enter the strings:

Enter first string: TecNStuff
Enter second string: Debian
Strings are not equal.

It also allows you to use the logical and && and or || to compare strings:

[[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"
Not equal

Check if a String Contains a Substring#

You can check that if a string contains a substring by multiple ways. It’s a simple method using asterisk * symbols to match all characters.

#!/bin/bash

VAR='Lion is a King of forest.'
if [[ $VAR == *"Lion"* ]]; then
  echo "It's contains."
fi

The script will echo the following:

It's contains.

You can check it by another ways using the regex operator =~ as given below:

#!/bin/bash

VAR='Lion is a King of forest.'
if [[ $VAR =~ .*Lion.* ]]; then
  echo "It's contains."
fi

The period followed by an asterisk .* matches zero or more occurrences any character except a newline character.

Check if a String is Empty#

It’s a common requirement to check that whether a variable is an empty string or not. Use the -n and -z operators to check it:

#!/bin/bash

VAR=''
if [[ -z $VAR ]]; then
  echo "String is empty."
fi
String is empty.
#!/bin/bash

VAR='Linuxize'
if [[ -n $VAR ]]; then
  echo "String is not empty."
fi
String is not empty.

Comparing Strings with the Case Operator#

You can use the case statement to compare strings, instead of using the test operators:

#!/bin/bash

VAR="Arch Linux"

case $VAR in

  "Arch Linux")
    echo -n "Linuxize matched"
    ;;

  Fedora | CentOS)
    echo -n "Red Hat"
    ;;
esac

Conclusion#

You learned how to compare two strings in bash scripts.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: bash, terminal

How to Install and Configure Squid Proxy on Ubuntu 20.04

Prev Post

Gunzip Command in Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 606
  • 1,055,378

DesignLinux.com © All rights reserved

Go to mobile version