Nested if/then Condition Tests

Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting
Prev | Chapter 7. Tests | Next

# 7.4. Nested `if/then` Condition Tests

Condition tests using the `if/then` construct may be nested. The net result is equivalent to using the && compound comparison operator.

       1 a=3
       2
       3 if [ "$a" -gt 0 ]
       4 then
       5   if [ "$a" -lt 5 ]
       6   then
       7     echo "The value of \"a\" lies somewhere between 0 and 5."
       8   fi
       9 fi
      10
      11 # Same result as:
      12
      13 if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]
      14 then
      15   echo "The value of \"a\" lies somewhere between 0 and 5."
      16 fi

Example 37-4 and Example 17-11 demonstrate nested `if/then` condition tests.

Prev | Home | Next
Other Comparison Operators | Up | Testing Your Knowledge of Tests