Nested if/then Condition Tests

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.

a=3

if [ "$a" -gt 0 ]
then
  if [ "$a" -lt 5 ]
  then
    echo "The value of \"a\" lies somewhere between 0 and 5."
  fi
fi

# Same result as:

if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]
then
  echo "The value of \"a\" lies somewhere between 0 and 5."
fi

Example 34-4 demonstrates a nested if/then condition test.


191 visits (1 today, 3 this week, 27 this month, 191 this year)
Uptime: 02:08:53 up 13 days, 7:37, 2 users, load average: 0.00, 0.02, 0.02
216.73.216.100 GET from server z.larrymurakami.com

Monday, July 14, 2025 @ 2:08:53 AM
z.ServerAdmin@lam1.us