Scripting: If Comparison Operators In Bash

27th October, 2016 by

In the previous part of the Getting Started With Scripting series we looked at using the if statement in Bash to make comparisons and using that to control program flow. In this article, we will be looking at the various types of comparison you can perform in Bash and how to do so.

First let’s look at some string test operators:

= or == is equal to

!= is not equal to

< is less than in ASCII alphabetical order

> is greater than in ASCII alphabetical order

-z test that the string is empty (null)

-n test that a string is not null

As we saw in the previous article the format to test for equality looks like:

if [ “$string” == “A string” ]

The variable being used for testing needs to be within quote marks so that when Bash replaces the variable with it’s contents that it is seen as a string for comparing. Both sides could be variables if desired, to allow comparing two string variables. The same format is also used to test not equal and less than/greater than. When testing for an empty or a not empty string the syntax is slightly different as you are only testing the one value:

if [ -z “$string” ]

Next let’s look at some integer (whole number) tests:

-eq is equal to

-ne is not equal to

-lt is less than

-le is less than or equal to

-gt is greater than

-ge is greater than or equal to

Note that these operators are different from their string counterparts despite performing the same type of test. They are used similarly though, for example:

if [ “$number” -eq “4” ]

if [ “$number” -gt “23” ]

Lastly, we have a number of handy test operators for files:

-e Does a file exist

-f test if a file

-d test if a directory

-L test if a symbolic link

-N if a file was modified after it was last read

-O if the current user owns the file

-G if the file’s group id matches the current user’s

-s test if a file has a size greater than 0

-r test if the file has read permission

-w test if the file has write permission

-x test if the file has execute permission

These can all be used in the same way that the -z test is used on a string. For these, the variable you should be providing, If you desire the inverse test ,then an exclamation point can be included in the test to invert it.

if [ -e “/home/myuser/myfile” ]

if [ ! -e “/home/myuser/myfile” ]

So the first line is true if the file “/home/myuser/myfile” exists, the second line is true if it does not.

We also have a few file comparison tests as well:

-nt tests if the file on the left is newer than the file on the right

-ot tests if the file on the left is older than the file on the right

-ef   tests if both files are hard links to the same file

These are used similar to the equality tests from before, so here’s an example:

if [ “$file” -nt “/home/myuser/myfile”]

if [ “$file” -ef “$otherfile” ]

We have covered the main comparison operators that you may need to use. As usual, this isn’t an exhaustive list, and there are some more operators (mostly for alternative file type tests) that are available if needed. This should serve as a handy reference for most of the comparison operations you’ll need to use to get started. In the next post, we’ll be looking at another method of manipulating program flow: loops.