0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.