Variable assignment (no spaces around =) in Bash Scripting - Time & Space Complexity
Let's see how the time it takes to run a simple variable assignment changes as we run it multiple times.
We want to know how the cost grows when we assign values repeatedly.
Analyze the time complexity of the following code snippet.
for i in {1..1000}
do
value=$i
echo $value
done
This code assigns a number to a variable and prints it, repeating 1000 times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Variable assignment and printing inside a loop.
- How many times: The loop runs exactly n times, where n is the number of iterations.
Each time we increase the number of loop runs, the total work grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments and prints |
| 100 | 100 assignments and prints |
| 1000 | 1000 assignments and prints |
Pattern observation: Doubling the input doubles the work done.
Time Complexity: O(n)
This means the time grows in a straight line with the number of assignments we do.
[X] Wrong: "Variable assignment is instant and does not add to time complexity."
[OK] Correct: Even simple assignments inside a loop add up as the loop runs more times, so total time grows with input size.
Understanding how simple repeated steps add up helps you explain script performance clearly and confidently.
"What if we replaced the loop with a function call inside the loop? How would the time complexity change?"
Practice
hello to a variable named greet in bash?Solution
Step 1: Understand bash variable assignment rules
Bash does not allow spaces around the=sign when assigning variables.Step 2: Check each option for spaces
Only greet=hello has no spaces around=. Options B, C, and D have spaces, which cause errors.Final Answer:
greet=hello -> Option DQuick Check:
No spaces around = means correct assignment [OK]
- Adding spaces around = causes syntax errors
- Using quotes unnecessarily in simple assignments
- Trying to assign with spaces after =
42 to the variable num?Solution
Step 1: Identify spaces around the equal sign
Bash requires no spaces around the=sign for variable assignment.Step 2: Verify each option
Only num=42 has no spaces around=. Others have spaces causing errors.Final Answer:
num=42 -> Option AQuick Check:
No spaces around = means correct syntax [OK]
- Leaving spaces around = causes syntax errors
- Confusing assignment with comparison operators
- Using quotes unnecessarily for numbers
name=John echo $name
Solution
Step 1: Understand variable assignment and usage
The variablenameis assigned the valueJohnwithout spaces around=.Step 2: Understand echo with variable
echo $nameprints the value stored inname, which isJohn.Final Answer:
John -> Option CQuick Check:
Variable value printed, not variable name [OK]
- Printing variable name instead of value
- Forgetting $ before variable in echo
- Adding spaces around = causing errors
count =10 echo $count
Solution
Step 1: Check variable assignment syntax
The assignmentcount =10has a space before=, which is not allowed in bash.Step 2: Understand effect of spaces
Spaces around=cause bash to treatcountas a command, leading to syntax error.Final Answer:
Spaces around = cause syntax error -> Option AQuick Check:
No spaces around = means valid assignment [OK]
- Adding spaces around = in assignment
- Forgetting $ when printing variable
- Assuming variable names can have spaces
year using the command date +%Y. Which of the following is the correct way to assign this output to year in bash?Solution
Step 1: Understand command substitution syntax
To assign command output, use$(command)without spaces around=.Step 2: Check each option for spaces and quotes
year=$(date +%Y) has no spaces around=and uses correct command substitution syntax. Options A and C have spaces causing errors. year= '$(date +%Y)' uses quotes which are unnecessary and may cause literal assignment.Final Answer:
year=$(date +%Y) -> Option BQuick Check:
No spaces and correct command substitution [OK]
- Adding spaces around = causes errors
- Using quotes around command substitution
- Forgetting to use $( ) for command output
Ready to prove it?
Pass the test and this pattern moves unknown
