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.


123 visits (1 today, 12 this week, 12 this month, 123 this year)
Uptime: 23:19:23 up 21 days, 6:54, 4 users, load average: 0.02, 0.03, 0.00
216.73.216.119 GET from server z.larrymurakami.com

Thursday, June 5, 2025 @ 11:19:23 PM
z.ServerAdmin@lam1.us