0
0
Bash Scriptingscripting~10 mins

[[ ]] extended test in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - [[ ]] extended test
Start
Evaluate [[ expression
Is expression true?
NoExecute else or skip
Yes
Execute then block
End
The [[ ]] extended test evaluates a condition. If true, it runs the then block; if false, it runs else or skips.
Execution Sample
Bash Scripting
if [[ -f /etc/passwd && -r /etc/passwd ]]; then
  echo "File exists and is readable"
else
  echo "File missing or not readable"
fi
Checks if /etc/passwd exists and is readable, then prints a message accordingly.
Execution Table
StepExpression EvaluatedResultBranch TakenOutput
1-f /etc/passwdtrueContinue
2-r /etc/passwdtrueContinue
3-f /etc/passwd && -r /etc/passwdtrueThen blockFile exists and is readable
4End of ifN/AExit
💡 Both conditions true, so then block executed and if statement ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
-f /etc/passwdN/Atruetruetrue
-r /etc/passwdN/AN/Atruetrue
Combined expressionN/AN/AN/Atrue
Key Moments - 2 Insights
Why do we use [[ ]] instead of [ ] for extended tests?
Because [[ ]] supports more complex expressions like &&, ||, and pattern matching, as shown in steps 1-3 of the execution_table.
What happens if one condition in && is false?
The combined expression becomes false and the else block runs or the if is skipped, as the logical AND requires both true (see exit_note).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the expression '-r /etc/passwd' at step 2?
Aerror
Bfalse
Ctrue
Dnot evaluated
💡 Hint
Check the 'Result' column in row for step 2 in execution_table.
At which step does the script decide to execute the then block?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Branch Taken' column to see when 'Then block' is chosen.
If the file /etc/passwd was not readable, how would the combined expression result change?
AIt would be true
BIt would be false
CIt would cause an error
DIt would be skipped
💡 Hint
Recall that && requires both conditions true; see key_moments about false condition effect.
Concept Snapshot
[[ ]] extended test syntax:
if [[ condition ]]; then
  commands
fi

Supports complex tests with &&, ||, pattern matching.
Returns true or false to control flow.
Use for safer, more powerful condition checks.
Full Transcript
This example shows how the bash [[ ]] extended test works. The script checks if the file /etc/passwd exists and is readable using two conditions combined with && inside [[ ]]. Step 1 tests if the file exists (-f), which is true. Step 2 tests if the file is readable (-r), also true. Step 3 combines both with &&, resulting in true, so the then block runs and prints 'File exists and is readable'. If either condition was false, the else block would run or the if would skip. The [[ ]] syntax allows combining tests safely and clearly. This trace helps beginners see each step's result and understand how the condition controls the script flow.