Unix/Linux shell script reference page (shell cheat sheet)

This is my Linux shell “cheat sheet.” I use it as a reference page, primarily when I’m writing Unix/Linux shell scripts. Most of the syntax works with the Bourne shell, though some operators make only work with Bash.

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.

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

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.

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

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

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

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

Bourne shell for loops

Here are a couple of examples of the Bourne shell for loop syntax. First, this one shows how to process every line a file:

file=myfile.txt

# handle line breaks properly; needed when a line has spaces or tabs
IFS=$'\n'

for i in `cat $file`
do
    # add your logic here
    echo "$i"
done

Next, this for loop example shows how to run the sbt clean command under every directory that is a subdirectory of the current directory, but only if the build.sbt file exists in those subdirectories:

for d in `ls -d1 *`
do
    cd $d
    # only run 'sbt clean' if this file exists
    if [ -e build.sbt]
    then
        sbt clean
    fi
    cd -
done

As shown in those two examples, the Bourne shell for loop syntax is this:

for ...
do
    # do whatever you want
done

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.

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