0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 modes available
Concept Flow - Integer comparisons (-eq, -ne, -gt, -lt, -ge, -le)
Start
Set two integers
Choose comparison operator
Compare integers
True?
YesExecute 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.