Integer comparisons help you check if numbers are equal, different, or which one is bigger. This is useful to make decisions in scripts.
Integer comparisons (-eq, -ne, -gt, -lt, -ge, -le) in Bash Scripting
[ number1 -operator number2 ]
Use spaces around brackets and operators.
Operators: -eq (equal), -ne (not equal), -gt (greater than), -lt (less than), -ge (greater or equal), -le (less or equal).
[ 5 -eq 5 ]
[ 3 -ne 4 ]
[ 7 -gt 2 ]
[ 1 -le 1 ]
This script compares two numbers and prints if the first is equal, less, or greater than the second.
#!/bin/bash num1=10 num2=20 if [ "$num1" -eq "$num2" ]; then echo "$num1 is equal to $num2" elif [ "$num1" -lt "$num2" ]; then echo "$num1 is less than $num2" else echo "$num1 is greater than $num2" fi
Always put spaces around the brackets and operators, or the comparison will fail.
Use integer comparisons only for whole numbers, not decimals.
For string comparisons, use different operators like = or !=.
Integer comparisons let you check relationships between numbers in bash scripts.
Use -eq, -ne, -gt, -lt, -ge, and -le inside [ ] with spaces.
They help your script make decisions based on numbers.
Practice
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 DQuick Check:
Equality check = -eq [OK]
- Confusing -eq with -ne
- Using == instead of -eq in [ ]
- Forgetting spaces around operators
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 BQuick Check:
Greater than check uses -gt [OK]
- Using > instead of -gt inside [ ]
- Using => instead of -ge
- Missing spaces around brackets and operators
num=5 if [ $num -lt 10 ]; then echo "Less" else echo "More" fi
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 AQuick Check:
5 < 10 is true, prints Less [OK]
- Confusing -lt with -le
- Forgetting spaces inside [ ]
- Using > instead of -lt
count=3 if [ $count -gt 5 ] then echo "High" fi
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 AQuick Check:
if condition must end with newline or semicolon before then [OK]
- Writing if [ condition ] then on same line without semicolon
- Forgetting spaces inside [ ]
- Using > instead of -gt
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 CQuick Check:
Use && with separate [ ] for clear inclusive range [OK]
- Using -o (OR) instead of AND
- Using > and < instead of -ge and -le
- Combining multiple conditions inside one [ ] with -a (less clear)
Ready to prove it?
Pass the test and this pattern moves unknown
