Shell script test syntax

Shell script test syntax

All of the shell script tests that follow should be performed between the bracket characters [ and ], like this:

if [ true ]
then
# do something here
fi

Make sure you leave spaces around the bracket characters.

File-related tests
==================

To perform tests on files use the following comparison operators:

-d file Test if file is a directory
-e file Test if file exists
-f file Test if file is an ordinary file
-r file Test if file is readable
-w file Test if file is writable
-x file Test if file is executable

As an example, assuming you have a file named foo, here’s how you would test to see if that file is readable:

if [ -r foo ]
then
# do something here
fi

String comparison tests
=======================

Here are the operators for performing string comparison tests:

s1 Test if s1 is not the empty string
s1 = s2 Test if s1 equals s2
s1 != s2 Test if s1 is not equal to s2
-n s1 Test if s1 has non-zero size
-z s1 Test if s1 has zero size

Here’s an example of how to see if two strings are equal:

if [ $foo = $bar ]
then
# do something
fi

This script echoes TRUE:

s1=

if [ -n $s1 ]
then
echo "TRUE"
else
echo "FALSE"
fi

This script echoes FALSE:

s1=bar

if [ -z "$s1" ]
then
echo "TRUE"
else
echo "FALSE"
fi

Those tests also showed the else syntax of the if statement.

Math/number equality tests
==========================

n1 -eq n2 Test if n1 equals n2
n1 -ne n2 Test if n1 is not equal to n2
n1 -lt n2 Test if n1 is less than n2
n1 -le n2 Test if n1 is less than or equal to n2
n1 -gt n2 Test if n1 is greater than n2
n1 -ge n2 Test if n1 is greater than or equal to n2

Here’s an example of how to test whether two numbers are equal:

if [ $n1 -eq $n2 ]
then
# do something
fi

Boolean and/or/not operators
============================

-a and
-o or
! not

Here’s an example of how to test perform a test using the and operator:

if [ $num -gt 0 -a $num -lt 10 ]
then
# do something here
fi

More powerful grouping operators
================================

If you need to perform multiple tests at one time you can use grouping operators, as shown in the example below.

a=5
b=20

if test \( $a -gt 0 -a $a -lt 10 \) -o \( $b -gt 0 -a $b -lt 20 \)
then
echo "TRUE"
else
echo "FALSE"
fi

That script echoes "TRUE".

Bourne shell arithmetic
=======================

In the Bourne shell math/arithmetic is performed using the expr command, like this:

sum=`expr $foo + $bar`
half=`expr $foo / 2`
times=`expr $foo \* 2`

Note that you can’t have any spaces before or after the equal sign in those (or any) shell script assignment statements.

A few other common shell tricks
===============================

Here are a few other tricks/techniques you will often see in Unix shell scripts:

cmd1 && cmd2 Run cmd1; if it returns 0 (success), run cmd2
cmd1 || cmd2 Run cmd1; if it returns non-zero, run cmd2
cmd1 & cmd2 Run cmd1 and also cmd2
(ls -1) Run the command "ls -1" in a subshell

Leave a Reply

Your email address will not be published. Required fields are marked *