Single quotes (literal strings) in Bash Scripting - Time & Space Complexity
We want to see how the time it takes to run a bash script changes when using single quotes for strings.
Specifically, does using single quotes affect how long the script runs as input grows?
Analyze the time complexity of the following code snippet.
for i in {1..n}
do
echo 'This is a literal string with no variables'
done
This script prints the same literal string n times using single quotes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs and prints the literal string each time.
- How many times: Exactly n times, where n is the input size.
Each time the loop runs, it prints the same fixed string without extra processing.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: The number of operations grows directly with n, one print per loop.
Time Complexity: O(n)
This means the time to run grows in a straight line as the number of loops increases.
[X] Wrong: "Using single quotes makes the script run faster because it skips variable checks."
[OK] Correct: The loop still runs n times and prints each time, so the main cost is the number of loops, not the quotes.
Understanding how loops and string handling affect script speed helps you write clear and efficient bash scripts in real tasks.
"What if we changed single quotes to double quotes with variables inside? How would the time complexity change?"