The -eq operator checks if two integers are equal. It returns true if they are the same number.
Click to reveal answer
beginner
How do you check if two numbers are not equal in bash?
Use the -ne operator. It returns true if the two integers are different.
Click to reveal answer
intermediate
What is the difference between -gt and -ge in bash?
-gt means 'greater than' (strictly bigger), while -ge means 'greater than or equal to' (bigger or the same).
Click to reveal answer
beginner
Write a bash condition to check if variable x is less than 10.
You can write: if [ "$x" -lt 10 ]; then to check if x is less than 10.
Click to reveal answer
intermediate
Why should you use integer comparison operators instead of string comparison for numbers in bash?
Integer operators correctly compare numeric values, while string comparison compares characters and can give wrong results for numbers.
Click to reveal answer
Which operator checks if two integers are equal in bash?
A-eq
B-ne
C-gt
D-lt
✗ Incorrect
The -eq operator tests if two integers are equal.
What does the -ne operator do?
AChecks if two numbers are equal
BChecks if one number is greater than another
CChecks if two numbers are not equal
DChecks if one number is less than another
✗ Incorrect
-ne means 'not equal' for integer comparison.
Which operator means 'greater than or equal to'?
A-gt
B-ge
C-le
D-lt
✗ Incorrect
-ge means 'greater than or equal to'.
How would you check if variable num is less than 5?
A[ "$num" -ne 5 ]
B[ "$num" -gt 5 ]
C[ "$num" -eq 5 ]
D[ "$num" -lt 5 ]
✗ Incorrect
Use -lt to check if a number is less than another.
Why avoid using string comparison operators for numbers in bash?
AThey compare characters, not numeric values
BThey only work with letters
CThey cause syntax errors
DThey are slower
✗ Incorrect
String comparison compares characters, which can give wrong results for numbers.
Explain how to compare two integers in bash using the operators -eq, -ne, -gt, -lt, -ge, and -le.
Think about how you check if numbers are equal, not equal, bigger, or smaller.
You got /4 concepts.
Describe why integer comparison operators are important in bash scripting and give an example where string comparison would fail.
Consider how numbers behave differently as strings.
You got /4 concepts.
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
Step 1: Understand the meaning of each operator
-eq means equal, -ne means not equal, -gt means greater than, -lt means less than.
Step 2: Identify the operator for equality
The operator that checks if two integers are equal is -eq.
Final Answer:
-eq -> Option D
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
Step 1: Recall integer comparison operators in bash
For greater than, bash uses -gt inside [ ]. The symbol > is for string comparison or redirection.
Step 2: Check each option
[ $x -gt 10 ] uses [ $x -gt 10 ] which is correct syntax for integer greater than check.
Final Answer:
[ $x -gt 10 ] -> Option B
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
Step 1: Evaluate the condition [ $num -lt 10 ]
num is 5, 5 is less than 10, so condition is true.
Step 2: Determine which branch runs
Since condition is true, the then branch runs and prints "Less".
Final Answer:
Less -> Option A
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
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.
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.
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.
Final Answer:
No error, script runs fine -> Option A
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
Step 1: Understand the range condition
We want age >= 18 and age <= 30 inclusive, so both conditions must be true.
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 [ ].
Final Answer:
[ $age -ge 18 ] && [ $age -le 30 ] -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.