Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Integer comparisons (-eq, -ne, -gt, -lt, -ge, -le) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Integer comparisons (-eq, -ne, -gt, -lt, -ge, -le)
Start
↓
Set two integers
↓
Choose comparison operator
↓
Compare integers
↓
True?
Yes→Execute True branch
↓
Execute False branch
↓
End
This flow shows how bash compares two integers using operators and executes code based on the comparison result.
Execution Sample
Bash Scripting
a=5
b=3
if [ $a -gt $b ]; then
  echo "a is greater"
else
  echo "a is not greater"
fi
This script compares two integers a and b and prints if a is greater than b.
Execution Table
StepVariable aVariable bConditionResultBranch TakenOutput
1535 -gt 3TrueTrue brancha is greater
253End of script---
💡 Comparison 5 -gt 3 is True, so the True branch runs and script ends after output.
Variable Tracker
VariableStartAfter Step 1Final
aunset55
bunset33
Key Moments - 3 Insights
Why do we use -gt instead of > in bash integer comparisons?
In bash, > is for string comparison or redirection, so integer comparisons use -gt, -lt, etc. as shown in the execution_table step 1.
What happens if variables are not integers?
If variables are not integers, the comparison fails or gives an error because -eq, -ne, -gt, -lt, -ge, -le expect integers, as seen in the condition column.
Why do we use square brackets [ ] around the condition?
Square brackets are a bash syntax for test command to evaluate conditions, as shown in the execution_sample code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 1, what is the result of the condition '5 -gt 3'?
AFalse
BTrue
CError
DUndefined
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does the script decide which branch to take?
AStep 2
BBefore Step 1
CStep 1
DAfter Step 2
💡 Hint
Look at the 'Branch Taken' column in execution_table row 1.
If variable a was 2 instead of 5, what would be the output?
Aa is not greater
Ba is greater
CNo output
DError
💡 Hint
Compare the condition 'a -gt b' with a=2 and b=3 logically.
Concept Snapshot
Bash integer comparisons use operators: -eq (equal), -ne (not equal), -gt (greater than), -lt (less than), -ge (greater or equal), -le (less or equal).
Syntax: [ $a -gt $b ] inside if or test.
Returns true or false to control script flow.
Use only for integers, not strings.
Square brackets [ ] run the test command.
Example: if [ $a -eq $b ]; then ... fi
Full Transcript
This lesson shows how bash scripts compare integers using operators like -eq, -ne, -gt, -lt, -ge, and -le. The script sets two variables a and b, then uses an if statement with [ $a -gt $b ] to check if a is greater than b. If true, it prints 'a is greater'; otherwise, it prints 'a is not greater'. The execution table traces the condition evaluation and branch taken. Key points include using -gt instead of > for integers, square brackets for test, and ensuring variables hold integers. The visual quiz tests understanding of condition results, branch decisions, and output changes if variables change.

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