[[ ]] extended test in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to run a bash script using the [[ ]] extended test changes as the input grows.
Specifically, we ask: how does the number of checks scale when testing multiple conditions?
Analyze the time complexity of the following code snippet.
#!/bin/bash
for file in *; do
if [[ -f "$file" && -r "$file" ]]; then
echo "$file is a readable file"
fi
done
This script loops over all items in the current folder and checks if each is a regular file and readable.
- Primary operation: The for-loop iterates over each file in the directory.
- How many times: Once for each file found (n times).
- Inside the loop: The [[ ]] test checks two conditions per file.
As the number of files increases, the script checks each file once with two conditions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 condition checks (2 per file) |
| 100 | About 200 condition checks |
| 1000 | About 2000 condition checks |
Pattern observation: The total checks grow linearly with the number of files.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the number of files checked.
[X] Wrong: "The [[ ]] test runs in constant time no matter how many files there are."
[OK] Correct: Each file requires its own check, so the total time grows with the number of files.
Understanding how loops and condition checks scale helps you write efficient scripts and explain your reasoning clearly in interviews.
What if we added a nested loop inside the for-loop that also checks each file? How would the time complexity change?
Practice
[[ ]] construct do in a bash script?Solution
Step 1: Understand the purpose of
The[[ ]][[ ]]syntax is used in bash to test conditions such as string comparison, number comparison, and file checks.Step 2: Compare with other options
Options B, C, and D describe other bash features unrelated to[[ ]].Final Answer:
It tests conditions like comparing strings or numbers. -> Option AQuick Check:
[[ ]]= condition test [OK]
- Confusing [[ ]] with function definition syntax
- Thinking [[ ]] creates variables
- Assuming [[ ]] runs commands
var equals "hello" using [[ ]]?Solution
Step 1: Identify correct string comparison operator in [[ ]]
In bash, inside[[ ]], the operator for string equality is==.-eqis for numeric equality.===is invalid.Step 2: Check each option
[[ $var != "hello" ]] checks inequality. [[ $var -eq "hello" ]] uses numeric-eqon string (error). [[ $var === "hello" ]] uses invalid===. [[ $var == "hello" ]] uses==correctly.Final Answer:
[[ $var == "hello" ]] -> Option BQuick Check:
String equality in [[ ]] uses == [OK]
- Using === which is invalid in bash
- Using -eq for string comparison
- Using != when checking for equality
var=5 if [[ $var -gt 3 && $var -lt 10 ]]; then echo "In range" else echo "Out of range" fi
Solution
Step 1: Evaluate numeric conditions inside [[ ]]
The variablevaris 5. The condition checks if 5 is greater than 3 and less than 10, which is true.Step 2: Determine which branch runs
Since the condition is true, the script echoes "In range".Final Answer:
In range -> Option AQuick Check:
5 > 3 and 5 < 10 = true [OK]
- Confusing -gt and -lt operators
- Using single & instead of &&
- Forgetting to quote variables (not critical here)
filename="test.txt" if [[ -f $filename && -r $filename ]] then echo "File exists and readable" fi
Solution
Step 1: Check syntax of [[ ]] condition
The condition starts with[[and has matching closing]]. So brackets are correct.Step 2: Verify other parts
Thethenkeyword is missing after the condition line, which causes a syntax error.Final Answer:
Missing then keyword -> Option DQuick Check:
Every if condition must be followed by then [OK]
- Forgetting the then keyword
- Using single [ with && without escaping
- Ignoring variable quoting warnings
input is either "yes" or "y" (case insensitive) using [[ ]]. Which snippet correctly does this?Solution
Step 1: Understand case-insensitive check
To ignore case, convertinputto lowercase using${input,,}before comparison.Step 2: Check logical OR condition
We want to accept either "yes" or "y", so use||between comparisons.Step 3: Evaluate options
[[ ${input,,} == "yes" || ${input,,} == "y" ]] correctly converts to lowercase and uses OR. [[ $input == "yes" || $input == "y" ]] lacks case conversion. [[ $input =~ ^(yes|y)$ ]] uses regex but is case-sensitive. [[ $input == "yes" && $input == "y" ]] uses AND which requires both to be true, impossible here.Final Answer:
[[ ${input,,} == "yes" || ${input,,} == "y" ]] -> Option CQuick Check:
Use ${var,,} for lowercase and || for OR [OK]
- Not converting input to lowercase
- Using && instead of || for alternatives
- Incorrect regex syntax inside [[ ]]
Ready to prove it?
Pass the test and this pattern moves unknown
