Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 modes available
Concept Flow - test command and [ ] syntax
Start
↓
Evaluate condition inside test or [
↓
Condition True?
No→Return 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.

Practice

(1/5)
1. What does the [ -f filename ] test check in a bash script?
easy
A. If filename is a directory
B. If filename is empty
C. If filename is executable
D. If filename exists and is a regular file

Solution

  1. Step 1: Understand the -f test

    The -f option in [ ] checks if the file exists and is a regular file (not a directory or special file).
  2. Step 2: Compare with other options

    -d checks for directory, -x for executable, and -s for non-empty file, so they don't match.
  3. Final Answer:

    If filename exists and is a regular file -> Option D
  4. Quick Check:

    -f means regular file check [OK]
Hint: Remember: -f means regular file exists [OK]
Common Mistakes:
  • Confusing -f with -d for directories
  • Forgetting spaces inside [ ]
  • Using filename without quotes causing errors
2. Which of the following is the correct syntax to test if variable var is equal to string hello in bash?
easy
A. [ $var = hello ]
B. [ $var == hello ]
C. [ "$var" = "hello" ]
D. [ "$var" == hello ]

Solution

  1. Step 1: Check quoting rules

    Variables should be quoted to avoid errors if empty or contain spaces, so "$var" is correct.
  2. Step 2: Check operator correctness

    Single equals = is POSIX compliant for string comparison inside [ ]. Double equals == works in [[ ]] but not always in [ ].
  3. Final Answer:

    [ "$var" = "hello" ] -> Option C
  4. Quick Check:

    Quote variables and use = inside [ ] [OK]
Hint: Always quote variables and use = inside [ ] [OK]
Common Mistakes:
  • Not quoting variables causing syntax errors
  • Using == inside [ ] which may fail
  • Missing spaces around brackets
3. What will be the output of this script snippet?
var=""
if [ -z "$var" ]; then
  echo "Empty"
else
  echo "Not Empty"
fi
medium
A. Empty
B. Not Empty
C. Syntax Error
D. No output

Solution

  1. Step 1: Understand the -z test

    -z returns true if the string length is zero (empty string).
  2. Step 2: Check variable value

    Variable var is set to empty string "", so [ -z "$var" ] is true and the if branch runs.
  3. Final Answer:

    Empty -> Option A
  4. Quick Check:

    -z tests empty string [OK]
Hint: Use -z to check empty strings [OK]
Common Mistakes:
  • Not quoting variable causing errors
  • Confusing -z with -n
  • Expecting else branch to run
4. Identify the error in this bash condition:
if [ $num -gt 10 ]; then
  echo "Greater"
fi
medium
A. Missing spaces inside [ ]
B. Variable $num should be quoted
C. Wrong operator for numeric comparison
D. Missing then keyword

Solution

  1. Step 1: Check variable usage in test

    When using variables in [ ], they should be quoted to avoid errors if empty or unset.
  2. Step 2: Analyze the given code

    The code uses $num unquoted. If num is empty, the test becomes invalid syntax.
  3. Final Answer:

    Variable $num should be quoted -> Option B
  4. Quick Check:

    Always quote variables in [ ] to avoid syntax errors [OK]
Hint: Quote variables inside [ ] to prevent errors [OK]
Common Mistakes:
  • Not quoting variables causing syntax errors
  • Confusing numeric and string operators
  • Forgetting spaces around brackets
5. You want to check if a file data.txt exists and is not empty before processing it in a script. Which condition correctly does this?
hard
A. [ -f data.txt ] && [ -s data.txt ]
B. [ -f data.txt -o -s data.txt ]
C. [ -f data.txt && -s data.txt ]
D. [ -f data.txt ] || [ -s data.txt ]

Solution

  1. Step 1: Understand the tests

    -f checks if file exists and is regular file; -s checks if file size is greater than zero (not empty).
  2. Step 2: Choose correct logical operator

    To require both conditions, use logical AND. Inside a single [ ], use -a; or chain separate tests with &&. Using && directly inside [ ] causes syntax error.
  3. Step 3: Analyze options

    The option with && inside single [ ] has syntax error. Chained tests with && are clear and reliable. Options using -o or || apply OR logic, incorrect.
  4. Final Answer:

    [ -f data.txt ] && [ -s data.txt ] -> Option A
  5. Quick Check:

    Use && to combine separate [ ] tests [OK]
Hint: Use separate [ ] with && for AND conditions [OK]
Common Mistakes:
  • Using -o or || which means OR, not AND
  • Using && inside a single [ ] (syntax error)
  • Not checking file size with -s
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