test command and [ ] syntax in Bash Scripting - Time & Space Complexity
We want to understand how the time taken by bash test commands grows as we check more conditions.
How does the number of checks affect the total time?
Analyze the time complexity of the following bash snippet using test or [ ] syntax.
for file in *; do
if [ -f "$file" ]; then
echo "$file is a regular file"
fi
done
This code loops over all items in the current folder and tests if each is a regular file.
Look for repeated actions in the code.
- Primary operation: The test command [ -f "$file" ] runs once per file.
- How many times: It runs once for each item in the folder, so as many times as there are files.
As the number of files grows, the number of test commands grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 test commands |
| 100 | 100 test commands |
| 1000 | 1000 test commands |
Pattern observation: The number of tests grows directly with the number of files.
Time Complexity: O(n)
This means the time grows in a straight line as the number of files increases.
[X] Wrong: "The test command runs only once regardless of files."
[OK] Correct: The test runs inside the loop, so it runs once per file, not just once total.
Understanding how loops and tests scale helps you write efficient scripts and answer questions about script speed.
"What if we added a nested loop inside that also runs tests? How would the time complexity change?"
Practice
[ -f filename ] test check in a bash script?Solution
Step 1: Understand the
The-ftest-foption in[ ]checks if the file exists and is a regular file (not a directory or special file).Step 2: Compare with other options
-dchecks for directory,-xfor executable, and-sfor non-empty file, so they don't match.Final Answer:
If filename exists and is a regular file -> Option DQuick Check:
-fmeans regular file check [OK]
- Confusing -f with -d for directories
- Forgetting spaces inside [ ]
- Using filename without quotes causing errors
var is equal to string hello in bash?Solution
Step 1: Check quoting rules
Variables should be quoted to avoid errors if empty or contain spaces, so "$var" is correct.Step 2: Check operator correctness
Single equals=is POSIX compliant for string comparison inside [ ]. Double equals==works in [[ ]] but not always in [ ].Final Answer:
[ "$var" = "hello" ] -> Option CQuick Check:
Quote variables and use = inside [ ] [OK]
- Not quoting variables causing syntax errors
- Using == inside [ ] which may fail
- Missing spaces around brackets
var="" if [ -z "$var" ]; then echo "Empty" else echo "Not Empty" fi
Solution
Step 1: Understand the
-ztest-zreturns true if the string length is zero (empty string).Step 2: Check variable value
Variablevaris set to empty string "", so[ -z "$var" ]is true and the if branch runs.Final Answer:
Empty -> Option AQuick Check:
-ztests empty string [OK]
- Not quoting variable causing errors
- Confusing -z with -n
- Expecting else branch to run
if [ $num -gt 10 ]; then echo "Greater" fi
Solution
Step 1: Check variable usage in test
When using variables in [ ], they should be quoted to avoid errors if empty or unset.Step 2: Analyze the given code
The code uses$numunquoted. Ifnumis empty, the test becomes invalid syntax.Final Answer:
Variable $num should be quoted -> Option BQuick Check:
Always quote variables in [ ] to avoid syntax errors [OK]
- Not quoting variables causing syntax errors
- Confusing numeric and string operators
- Forgetting spaces around brackets
data.txt exists and is not empty before processing it in a script. Which condition correctly does this?Solution
Step 1: Understand the tests
-fchecks if file exists and is regular file;-schecks if file size is greater than zero (not empty).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.Step 3: Analyze options
The option with&&inside single[ ]has syntax error. Chained tests with&&are clear and reliable. Options using-oor||apply OR logic, incorrect.Final Answer:
[ -f data.txt ] && [ -s data.txt ] -> Option AQuick Check:
Use && to combine separate [ ] tests [OK]
- Using -o or || which means OR, not AND
- Using && inside a single [ ] (syntax error)
- Not checking file size with -s
Ready to prove it?
Pass the test and this pattern moves unknown
