Unix/Linux shell script reference page

Back to top

Linux 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

Very important: Make sure you leave spaces around the bracket characters.

I'll show more detailed tests as we go along.

Back to top

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
Back to top

Linux shell 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.

Back to top

Linux shell script math/number equality tests

Here's how you perform math/number/arithmetic tests using the Bourne and Bash shells:

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
Back to top

Linux shell boolean and/or/not operators

The following boolean and/or/not operators can also be used in your tests:

-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
Back to top

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".

Back to top

Linux Bourne shell if, then, else, else if (elif) syntax

One thing that varies from one programming language to another is the if / then / else / else if / elseif syntax. In the case of the Bourne shell, the "else if" keyword is actually "elif", so a sample Bourne shell if then else if statement looks like this: 

if [ -e 'foo' ]
then
  echo "if was true"
elif [ -e 'bar' ]
then
  echo "elif was true"
else
  echo "came down to else"
fi

(Thanks to the commenter below for suggesting this addition to this page.)

Back to top

Linux 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`

# increment a counter
(( count++ ))

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

Back to top

A few other common Linux 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
Back to top
Permalink

thats a good amount of info for a beginner..

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.