Bird
0
0

You want to write a bash script that prints "Valid" only if a variable age is between 18 and 30 inclusive. Which condition correctly checks this?

hard🚀 Application Q15 of 15
Bash Scripting - Conditionals
You want to write a bash script that prints "Valid" only if a variable age is between 18 and 30 inclusive. Which condition correctly checks this?
A[ $age -ge 18 -a $age -le 30 ]
B[ $age -gt 18 -o $age -lt 30 ]
C[ $age -ge 18 ] && [ $age -le 30 ]
D[ $age -gt 18 -a $age -lt 30 ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range condition

    We want age >= 18 and age <= 30 inclusive, so both conditions must be true.
  2. Step 2: Analyze each option

    [ $age -ge 18 -a $age -le 30 ] uses -a inside single [ ], which is valid but less recommended. [ $age -gt 18 -o $age -lt 30 ] uses -o (or), which is wrong. [ $age -ge 18 ] && [ $age -le 30 ] uses two separate [ ] with &&, which is correct and clear. [ $age -gt 18 -a $age -lt 30 ] uses > and < (not inclusive) and -a inside [ ].
  3. Final Answer:

    [ $age -ge 18 ] && [ $age -le 30 ] -> Option C
  4. Quick Check:

    Use && with separate [ ] for clear inclusive range [OK]
Quick Trick: Use && with separate [ ] for AND conditions [OK]
Common Mistakes:
MISTAKES
  • Using -o (OR) instead of AND
  • Using > and < instead of -ge and -le
  • Combining multiple conditions inside one [ ] with -a (less clear)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes