Comparing Strings

Comparing Strings using "if"

Testing Strings


There are many string comparisons that can be made using Bash. These usually take the form of comparing one variable against another or one variable against a string of text or simply comparing text against text.



String Comparison Tests Available:


Test True if
[ STRING1 == STRING2 ] STRING1 is equal to STRING2
[ STRING1 = STRING2 ] STRING1 is equal to STRING2
[ STRING1 != STRING2 ] STRING1 is NOT equal to STRING2
[ STRING1 \> STRING2 ] STRING1 is lexically greater than STRING2 - b is greater than a
[ STRING1 \< STRING2 ] STRING1 is lexically less than STRING2 - a is less than b
[ -n STRING ] STRING is non zero - a variable has been set
[ -z STRING ] STRING is zero - variable has no value
[[ STRING1 =~ REGEXPRESSION ]] STRING1 matches Regular Expression

Single Brackets [ ]


So far we have seen numerous examples of test that can be carried out on files and variables. In these examples we have used a single square bracket syntax "[ ..... ]". Later we will look at some examples of using the double bracket syntax. Using the single bracket syntax tests can be carried out against files, strings and number based conditions.
(We will look at tests on numbers in another section Testing Numerical Values)

Examples:
A simple test for the presence of a file:



if [ -f /home/john/scripts/myfile.txt ]; then
   echo "File Found"
fi

A simple string comparison of two variables:



string1=landoflinux
string2=landoflinux

if [ $string1==$string2 ];then
   echo "string1: $string1 is the same as string2: $string2"
fi

The above would result in the echo statement being executed.

An example of a "lexically test"



#!/bin/bash

string1=aa
string2=bb

if [ $string1\<$string2 ];then
   echo "string1 is lexically less than string2"
else
   echo "string1 is not lexically less than string2"
fi

In the above test, "aa" is lexically less than "bb".



Double Brackets [[ ]]


The double bracket syntax allows for several additional options to be tested. Using the double bracket syntax allows us to use basic shell "globbing". This means that you cold use a "*". Regular expressions may also be sued within the double bracket syntax. Another feature of the double bracket option is that it does not expand filenames.

Examples:



if [[ "$name"==*john* ]];then

If the variable "$name" contains the string "john" then the statement is true. To illustrate this we can run the following:



#!/bin/bash

name=testjohntext

if [[ "$name"==*john* ]]; then
   echo "The string \"john\" was found in the variable \$name"
fi

Output from the above test:



john@john-desktop:~/scripts$ ./test07.sh 
The string "john" was found in the variable $name

Using Regular Expressions within Double Brackets


Another often use of the Double Bracket syntax is the use of regular expressions. A simple example of a regular expression being used within a double bracket can be seen below:

Regular Expression Double Bracket Example:



#!/bin/bash
mytest="The quick brown fox"
echo "The variable \$mytest contains $mytest"

if [[ "$mytest"=~The..uick.b..w* ]];then
   echo "We found a match"
fi

Output from the above example:



john@john-desktop:~/scripts$ ./test08.sh
The variable $mytest contains The quick brown fox
We found a match