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.


6 visits (2 today, 3 this week, 6 this month, 6 this year)
Uptime: 09:59:54 up 10 days, 19:23, 2 users, load average: 0.20, 0.11, 0.03
3.136.234.97 GET from server z.lamurakami.com

Tuesday, March 11, 2025 @ 9:59:54 AM
z.ServerAdmin@lam1.us