Indexed array declaration in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to create an indexed array grows as the number of elements increases.
Specifically, how does declaring and assigning values to an array behave when the array size changes?
Analyze the time complexity of the following code snippet.
# Declare an indexed array and assign values
my_array=()
for ((i=1; i<=n; i++)); do
my_array+=($i)
done
This code creates an empty indexed array and then adds numbers from 1 to n one by one.
- Primary operation: Adding one element to the array inside the loop.
- How many times: The loop runs n times, once for each element added.
Each new element is added one after another, so the total work grows as we add more elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to declare and fill the array grows linearly with the number of elements.
[X] Wrong: "Adding elements to an array is instant and does not depend on the number of elements."
[OK] Correct: Each addition takes time, so more elements mean more total time, even if each step is quick.
Understanding how loops and array operations scale helps you write efficient scripts and explain your code clearly in interviews.
"What if we declared the array with all elements at once instead of adding them one by one? How would the time complexity change?"
Practice
fruits with values apple, banana, and cherry in Bash?Solution
Step 1: Understand Bash array declaration syntax
Bash arrays use parentheses with space-separated values, no commas or braces.Step 2: Check each option's syntax
fruits=(apple banana cherry) uses parentheses and spaces correctly. Square brackets and braces are incorrect. fruits=("apple, banana, cherry") treats all values as one string.Final Answer:
fruits=(apple banana cherry) -> Option DQuick Check:
Parentheses + spaces = correct array declaration [OK]
- Using square brackets instead of parentheses
- Including commas between values
- Putting all values inside quotes as one string
numbers in Bash?Solution
Step 1: Recall empty array declaration in Bash
An empty indexed array is declared with empty parentheses.Step 2: Evaluate each option
numbers=() uses empty parentheses correctly. Braces and square brackets are invalid. numbers="" assigns an empty string, not an array.Final Answer:
numbers=() -> Option BQuick Check:
Empty parentheses declare empty array [OK]
- Using square brackets or braces instead of parentheses
- Assigning empty string instead of array
- Confusing array declaration with variable assignment
colors=(red green blue), what will be the output of echo ${colors[1]}?Solution
Step 1: Understand array indexing in Bash
Indexed arrays start at 0, so index 1 is the second element.Step 2: Identify the element at index 1
colors[0] = red, colors[1] = green, colors[2] = blue.Final Answer:
green -> Option AQuick Check:
Index 1 = second element = green [OK]
- Thinking array starts at 1 instead of 0
- Printing the variable name instead of value
- Confusing index with value
myarray=(one two three four five six seven eight nine ten
Solution
Step 1: Check array syntax completeness
The declaration starts with '(' but does not have a closing ')'.Step 2: Verify other syntax rules
Values do not need quotes if no spaces, array name is valid, commas are not used in Bash arrays.Final Answer:
Missing closing parenthesis -> Option CQuick Check:
Parentheses must open and close [OK]
- Forgetting the closing parenthesis
- Adding commas between values
- Quoting values unnecessarily
days with the values Mon, Tue, and Wed, then print the total number of elements in the array?Solution
Step 1: Declare the array correctly
Use parentheses with space-separated values: days=(Mon Tue Wed).Step 2: Use correct syntax to get array length
Use ${#days[@]} to get the number of elements in the array.Final Answer:
days=(Mon Tue Wed); echo ${#days[@]} -> Option AQuick Check:
Parentheses + ${#array[@]} = count [OK]
- Using square brackets for array declaration
- Using commas between values
- Using incorrect syntax for length like ${#days} or .length
Ready to prove it?
Pass the test and this pattern moves unknown
