Chapter 7. Tests

Every reasonably complete programming language can test for a condition, then act according to the result of the test. Bash has the test command, various bracket and parenthesis operators, and the if/then construct.

7.1. Test Constructs


Example 7-1. What is truth?

   1 #!/bin/bash
   2 
   3 echo
   4 
   5 echo "Testing \"0\""
   6 if [ 0 ]      # zero
   7 then
   8   echo "0 is true."
   9 else
  10   echo "0 is false."
  11 fi
  12 
  13 echo
  14 
  15 echo "Testing \"NULL\""
  16 if [ ]        # NULL (empty condition)
  17 then
  18   echo "NULL is true."
  19 else
  20   echo "NULL is false."
  21 fi
  22 
  23 echo
  24 
  25 echo "Testing \"xyz\""
  26 if [ xyz ]    # string
  27 then
  28   echo "Random string is true."
  29 else
  30   echo "Random string is false."
  31 fi
  32 
  33 echo
  34 
  35 echo "Testing \"\$xyz\""
  36 if [ $xyz ]   # Tests if $xyz is null, but...
  37               # it's only an uninitialized variable.
  38 then
  39   echo "Uninitialized variable is true."
  40 else
  41   echo "Uninitialized variable is false."
  42 fi
  43 
  44 echo
  45 
  46 echo "Testing \"-n \$xyz\""
  47 if [ -n "$xyz" ]            # More pedantically correct.
  48 then
  49   echo "Uninitialized variable is true."
  50 else
  51   echo "Uninitialized variable is false."
  52 fi
  53 
  54 echo
  55 
  56 xyz=                        # Initialized, but set to null value.
  57 
  58 echo "Testing \"-n \$xyz\""
  59 if [ -n "$xyz" ]
  60 then
  61   echo "Null variable is true."
  62 else
  63   echo "Null variable is false."
  64 fi
  65 
  66 
  67 echo
  68 
  69 
  70 # When is "false" true?
  71 
  72 echo "Testing \"false\""
  73 if [ "false" ]              #  It seems that "false" is just a string.
  74 then
  75   echo "\"false\" is true." #+ and it tests true.
  76 else
  77   echo "\"false\" is false."
  78 fi
  79 
  80 echo
  81 
  82 echo "Testing \"\$false\""  # Again, uninitialized variable.
  83 if [ "$false" ]
  84 then
  85   echo "\"\$false\" is true."
  86 else
  87   echo "\"\$false\" is false."
  88 fi                          # Now, we get the expected result.
  89 
  90 
  91 echo
  92 
  93 exit 0

Exercise. Explain the behavior of Example 7-1, above.

   1 if [ condition-true ]
   2 then
   3    command 1
   4    command 2
   5    ...
   6 else
   7    # Optional (may be left out if not needed).
   8    # Adds default code block executing if original condition tests false.
   9    command 3
  10    command 4
  11    ...
  12 fi

Add a semicolon when 'if' and 'then' are on same line.

   1 if [ -x "$filename" ]; then

Else if and elif

elif

elif is a contraction for else if. The effect is to nest an inner if/then construct within an outer one.

   1 if [ condition1 ]
   2 then
   3    command1
   4    command2
   5    command3
   6 elif [ condition2 ]
   7 # Same as else if
   8 then
   9    command4
  10    command5
  11 else
  12    default-command
  13 fi

The if test condition-true construct is the exact equivalent of if [ condition-true ]. As it happens, the left bracket, [ , is a token which invokes the test command. The closing right bracket, ] , in an if/test should not therefore be strictly necessary, however newer versions of Bash require it.

Note

The test command is a Bash builtin which tests file types and compares strings. Therefore, in a Bash script, test does not call the external /usr/bin/test binary, which is part of the sh-utils package. Likewise, [ does not call /usr/bin/[, which is linked to /usr/bin/test.

 bash$ type test
 test is a shell builtin
 bash$ type '['
 [ is a shell builtin
 bash$ type '[['
 [[ is a shell keyword
 bash$ type ']]'
 ]] is a shell keyword
 bash$ type ']'
 bash: type: ]: not found
 	      


Example 7-2. Equivalence of [ ] and test

   1 #!/bin/bash
   2 
   3 echo
   4 
   5 if test -z "$1"
   6 then
   7   echo "No command-line arguments."
   8 else
   9   echo "First command-line argument is $1."
  10 fi
  11 
  12 
  13 if [ -z "$1" ]    # Functionally identical to above code block.
  14 #   if [ -z "$1"   should work, but...
  15 #+  Bash responds to a missing close bracket with an error message.
  16 then
  17   echo "No command-line arguments."
  18 else
  19   echo "First command-line argument is $1."
  20 fi
  21 
  22 echo
  23 
  24 exit 0

The [[ ]] construct is the shell equivalent of [ ]. This is the extended test command, adopted from ksh88.

Note

No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion and command substitution.

   1 file=/etc/passwd
   2 
   3 if [[ -e $file ]]
   4 then
   5   echo "Password file exists."
   6 fi

Tip

Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, The &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.

Note

Following an if, neither the test command nor the test brackets ( [ ] or [[ ]] ) are strictly necessary.
   1 dir=/home/bozo
   2 
   3 if cd "$dir" 2>/dev/null; then   # "2>/dev/null" hides error message.
   4   echo "Now in $dir."
   5 else
   6   echo "Can't change to $dir."
   7 fi
The "if COMMAND" construct returns the exit status of COMMAND.

Similarly, a condition within test brackets may stand alone without an if, when used in combination with a list construct.
   1 var1=20
   2 var2=22
   3 [ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2"
   4 
   5 home=/home/bozo
   6 [ -d "$home" ] || echo "$home directory does not exist."

The (( )) construct expands and evaluates an arithmetic expression. If the expression evaluates as zero, it returns an exit status of 1, or "false". A non-zero expression returns an exit status of 0, or "true". This is in marked contrast to using the test and [ ] constructs previously discussed.


Example 7-3. Arithmetic Tests using (( ))

   1 #!/bin/bash
   2 # Arithmetic tests.
   3 
   4 # The (( ... )) construct evaluates and tests numerical expressions.
   5 # Exit status opposite from [ ... ] construct!
   6 
   7 (( 0 ))
   8 echo "Exit status of \"(( 0 ))\" is $?."   # 1
   9 
  10 (( 1 ))
  11 echo "Exit status of \"(( 1 ))\" is $?."   # 0
  12 
  13 (( 5 > 4 ))                                # true
  14 echo $?                                    # 0
  15 
  16 (( 5 > 9 ))                                # false
  17 echo $?                                    # 1
  18 
  19 exit 0