Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Integer comparisons (-eq, -ne, -gt, -lt, -ge, -le) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Integer Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash integer comparison?
Consider the following Bash script snippet. What will it print?
Bash Scripting
a=5
b=10
if [ $a -lt $b ]; then
  echo "a is less than b"
else
  echo "a is not less than b"
fi
Aa is less than b
Ba is not less than b
CNo output
DSyntax error
Attempts:
2 left
💡 Hint
Remember that -lt means 'less than' in Bash integer comparisons.
💻 Command Output
intermediate
2:00remaining
What output does this script produce with -ne operator?
What will this Bash script print?
Bash Scripting
x=7
y=7
if [ $x -ne $y ]; then
  echo "x and y are different"
else
  echo "x and y are the same"
fi
Ax and y are different
Bx and y are the same
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
The -ne operator means 'not equal'.
💻 Command Output
advanced
2:00remaining
What is the output of this script using multiple integer comparisons?
What will this Bash script print?
Bash Scripting
num=15
if [ $num -ge 10 ] && [ $num -le 20 ]; then
  echo "num is between 10 and 20"
else
  echo "num is outside the range"
fi
ASyntax error
Bnum is outside the range
Cnum is between 10 and 20
DNo output
Attempts:
2 left
💡 Hint
Check if num is greater or equal to 10 AND less or equal to 20.
💻 Command Output
advanced
2:00remaining
What error does this script raise due to incorrect integer comparison?
What happens when you run this Bash script?
Bash Scripting
val=abc
if [ $val -gt 5 ]; then
  echo "val is greater than 5"
else
  echo "val is not greater than 5"
fi
Aval is greater than 5
BSyntax error
Cval is not greater than 5
Dinteger expression expected error
Attempts:
2 left
💡 Hint
The variable val contains non-numeric characters.
💻 Command Output
expert
3:00remaining
What is the output of this complex integer comparison script?
Analyze the following Bash script and select the output it produces.
Bash Scripting
a=8
b=3
if [ $a -gt $b ]; then
  if [ $a -le 10 ]; then
    echo "a is greater than b and less or equal to 10"
  else
    echo "a is greater than b and greater than 10"
  fi
else
  echo "a is not greater than b"
fi
Aa is greater than b and less or equal to 10
Ba is greater than b and greater than 10
Ca is not greater than b
DSyntax error
Attempts:
2 left
💡 Hint
Check both conditions carefully in the nested if statements.

Practice

(1/5)
1. Which operator checks if two integers are equal in a bash script?
easy
A. -gt
B. -ne
C. -lt
D. -eq

Solution

  1. Step 1: Understand the meaning of each operator

    -eq means equal, -ne means not equal, -gt means greater than, -lt means less than.
  2. Step 2: Identify the operator for equality

    The operator that checks if two integers are equal is -eq.
  3. Final Answer:

    -eq -> Option D
  4. Quick Check:

    Equality check = -eq [OK]
Hint: Remember: eq means equal, ne means not equal [OK]
Common Mistakes:
  • Confusing -eq with -ne
  • Using == instead of -eq in [ ]
  • Forgetting spaces around operators
2. Which of the following is the correct syntax to check if variable x is greater than 10 in bash?
easy
A. [ $x > 10 ]
B. [ $x -gt 10 ]
C. [ $x -ge 10 ]
D. [ $x => 10 ]

Solution

  1. Step 1: Recall integer comparison operators in bash

    For greater than, bash uses -gt inside [ ]. The symbol > is for string comparison or redirection.
  2. Step 2: Check each option

    [ $x -gt 10 ] uses [ $x -gt 10 ] which is correct syntax for integer greater than check.
  3. Final Answer:

    [ $x -gt 10 ] -> Option B
  4. Quick Check:

    Greater than check uses -gt [OK]
Hint: Use -gt for greater than, not > inside [ ] [OK]
Common Mistakes:
  • Using > instead of -gt inside [ ]
  • Using => instead of -ge
  • Missing spaces around brackets and operators
3. What will be the output of this bash script snippet?
num=5
if [ $num -lt 10 ]; then
echo "Less"
else
echo "More"
fi
medium
A. Less
B. More
C. Syntax Error
D. No output

Solution

  1. Step 1: Evaluate the condition [ $num -lt 10 ]

    num is 5, 5 is less than 10, so condition is true.
  2. Step 2: Determine which branch runs

    Since condition is true, the then branch runs and prints "Less".
  3. Final Answer:

    Less -> Option A
  4. Quick Check:

    5 < 10 is true, prints Less [OK]
Hint: If condition true, then branch runs; else runs otherwise [OK]
Common Mistakes:
  • Confusing -lt with -le
  • Forgetting spaces inside [ ]
  • Using > instead of -lt
4. Identify the error in this bash snippet:
count=3
if [ $count -gt 5 ]
then
  echo "High"
fi
medium
A. No error, script runs fine
B. Missing semicolon or newline after condition
C. Missing spaces around brackets
D. Wrong operator -gt used

Solution

  1. Step 1: Check if the if condition syntax is correct

    The if condition line ends with ] but the next line is then. Bash requires a newline or semicolon after ] before then.
  2. Step 2: Identify the fix

    Add a newline (already present) or a semicolon after ] to separate condition and then. Here newline is present, so syntax is correct.
  3. Step 3: Re-examine the snippet

    Actually, the snippet as shown is correct syntax if newline is present after ] before then. If newline missing, error occurs.
  4. Final Answer:

    No error, script runs fine -> Option A
  5. Quick Check:

    if condition must end with newline or semicolon before then [OK]
Hint: Put newline or semicolon after ] before then [OK]
Common Mistakes:
  • Writing if [ condition ] then on same line without semicolon
  • Forgetting spaces inside [ ]
  • Using > instead of -gt
5. 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
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 ]

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]
Hint: Use && with separate [ ] for AND conditions [OK]
Common Mistakes:
  • Using -o (OR) instead of AND
  • Using > and < instead of -ge and -le
  • Combining multiple conditions inside one [ ] with -a (less clear)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard