Accessing variables ($var and ${var}) in Bash Scripting - Time & Space Complexity
When we use variables in bash scripts, we often access their values using $var or ${var} syntax.
We want to understand how the time it takes to access these variables changes as the script grows or uses more variables.
Analyze the time complexity of the following code snippet.
#!/bin/bash
var="Hello"
echo $var
var2="World"
echo ${var2}
for i in 1 2 3 4 5; do
echo $var
echo ${var2}
done
This script sets two variables and prints them once, then prints them inside a loop five times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing variables using $var and ${var2} inside the loop.
- How many times: The variable access happens 2 times per loop iteration, repeated 5 times.
Each loop iteration accesses variables twice. As the number of iterations grows, the total variable accesses grow proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 variable accesses |
| 100 | 200 variable accesses |
| 1000 | 2000 variable accesses |
Pattern observation: The number of variable accesses grows linearly with the number of loop iterations.
Time Complexity: O(n)
This means the time to access variables grows directly in proportion to how many times you access them in a loop.
[X] Wrong: "Accessing a variable like $var is instant and does not add to execution time as the script grows."
[OK] Correct: While each access is fast, if you access variables many times in loops, the total time adds up and grows with the number of accesses.
Understanding how variable access scales helps you write efficient scripts and shows you can think about performance even in simple tasks.
What if we replaced the loop with nested loops accessing variables multiple times? How would the time complexity change?
Practice
${var} instead of just $var?Solution
Step 1: Understand variable access syntax
In bash,$varaccesses the value of a variable named var.Step 2: Purpose of braces
Using${var}helps separate the variable name from other text, avoiding confusion when concatenating.Final Answer:
To clearly separate the variable name from surrounding text -> Option AQuick Check:
Braces separate variable name from text = A [OK]
- Thinking braces declare variables
- Using braces to comment variables
- Assuming braces change variable case
name followed immediately by the string _end in bash?Solution
Step 1: Understand variable and string concatenation
To print variablenamefollowed by_end, we must separate the variable name from the string.Step 2: Use braces to separate variable name
Using${name}_endcorrectly prints the variable value plus the string_end. Without braces,$name_endlooks for a variable namedname_end.Final Answer:
echo ${name}_end -> Option DQuick Check:
Braces separate variable name from text = C [OK]
- Forgetting braces and getting wrong variable
- Adding space breaks concatenation
- Using wrong variable name inside braces
var="world"
echo "Hello $var!"
echo "Hello ${var}!"
echo "Hello $var123"
echo "Hello ${var}123"Solution
Step 1: Analyze each echo line
First two lines print "Hello world!" because $var and ${var} both expand to "world". Third line prints "Hello " because $var123 looks for variable named var123 which is empty. Fourth line prints "Hello world123" because ${var} is "world" plus "123".Step 2: Combine outputs
Outputs are:
Hello world!
Hello world!
Hello
Hello world123Final Answer:
Hello world! Hello world! Hello Hello world123 -> Option BQuick Check:
Braces separate variable name correctly = D [OK]
- Assuming $var123 expands var plus 123
- Ignoring empty variable var123
- Confusing output lines order
filename="report" echo "File: $filename.txt"
Why does it not print
File: report.txt as expected?Solution
Step 1: Understand variable expansion with suffix
Using$filename.txtmakes bash look for variable namedfilename.txt, which does not exist.Step 2: Fix with braces
Using${filename}.txtcorrectly expandsfilenameand appends.txt.Final Answer:
Because $filename.txt looks for variable named filename.txt which is undefined -> Option CQuick Check:
Braces needed to separate variable name from suffix = A [OK]
- Thinking dot is part of variable name
- Assuming echo can't print dots
- Ignoring variable name rules
user followed by the string _backup only if user is not empty. Which is the best way to write the echo command?Solution
Step 1: Understand conditional variable expansion
The expression${user:+${user}_backup}prints${user}_backuponly ifuseris not empty.Step 2: Compare options
echo "${user}_backup" always prints_backupeven ifuseris empty. echo "$user_backup" looks for variableuser_backup. echo "${user}_backup" && [ -n "$user" ] has wrong syntax mixing echo and condition.Final Answer:
echo "${user:+${user}_backup}" -> Option AQuick Check:
Use ${var:+value} to print only if var set = B [OK]
- Using $user_backup instead of ${user}_backup
- Printing _backup even if user empty
- Mixing echo with condition incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
