0
0
Bash Scriptingscripting~10 mins

test command and [ ] syntax in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - test command and [ ] syntax
Start
Evaluate condition inside test or [
Condition True?
NoReturn 1 (False)
Yes
Return 0 (True)
Use return code for if/else or other logic
End
The test command or [ ] evaluates a condition and returns 0 if true, 1 if false, which scripts use to decide next steps.
Execution Sample
Bash Scripting
if [ 5 -gt 3 ]; then
  echo "Yes"
else
  echo "No"
fi
This script checks if 5 is greater than 3 and prints 'Yes' if true, otherwise 'No'.
Execution Table
StepCondition EvaluatedResultReturn CodeBranch TakenOutput
15 -gt 3True0Then branchYes
2End of if----
💡 Condition 5 -gt 3 is true, so return code 0 leads to 'then' branch execution.
Variable Tracker
VariableStartAfter Step 1Final
Return Code ($?)-00
Output-YesYes
Key Moments - 3 Insights
Why do we use spaces around [ and ] in the condition?
Because [ and ] are commands in bash, they need spaces to separate them from the condition; see execution_table step 1 where condition is evaluated correctly only with spaces.
What does the return code 0 or 1 mean in test or [ ]?
Return code 0 means condition is true, 1 means false; scripts use this to decide which branch to run, as shown in execution_table step 1.
Can we use test and [ ] interchangeably?
Yes, test and [ ] do the same thing; [ ] requires spaces and a closing ], test does not; both return codes behave the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 1, what is the return code when condition '5 -gt 3' is true?
A5
B1
C0
D-1
💡 Hint
Check the 'Return Code' column in execution_table row 1.
At which step does the script decide to print 'Yes'?
AStep 1
BStep 2
CBefore Step 1
DNo step prints 'Yes'
💡 Hint
Look at the 'Branch Taken' and 'Output' columns in execution_table row 1.
If the condition was '[ 2 -eq 3 ]', how would the return code change?
AReturn code would be 0
BReturn code would be 1
CReturn code would be 2
DReturn code would be -1
💡 Hint
Recall that return code 0 means true, 1 means false; 2 -eq 3 is false.
Concept Snapshot
test and [ ] check conditions in bash.
Return code 0 means true, 1 means false.
Spaces are required around [ and ]
Used in if statements to control flow.
Example: if [ 5 -gt 3 ]; then echo Yes; fi
Full Transcript
The test command or [ ] syntax in bash is used to check conditions. When you write a condition inside [ ], you must have spaces around the brackets because they are commands. The condition inside is evaluated, and if true, the command returns 0; if false, it returns 1. Scripts use this return code to decide which branch of code to run, like in an if statement. For example, if [ 5 -gt 3 ]; then echo Yes; else echo No; fi will print Yes because 5 is greater than 3. Remember, 0 means true, 1 means false. You can use test or [ ] interchangeably, but [ ] needs spaces and a closing bracket. This simple mechanism helps scripts make decisions based on conditions.