Integer variables in Bash Scripting - Time & Space Complexity
We want to see how the time to run a bash script changes when it uses integer variables.
Specifically, we ask: does using integer variables affect how long the script takes as it runs?
Analyze the time complexity of the following code snippet.
#!/bin/bash
count=0
for i in {1..1000}
do
((count = count + 1))
done
echo "Count is $count"
This script uses an integer variable count and increments it 1000 times in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Incrementing the integer variable
countinside the loop. - How many times: Exactly 1000 times, once per loop iteration.
As the number of loop iterations grows, the number of increments grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 increments |
| 100 | 100 increments |
| 1000 | 1000 increments |
Pattern observation: The work grows directly with the number of loop steps.
Time Complexity: O(n)
This means the time to run the script grows in a straight line as the number of increments increases.
[X] Wrong: "Using integer variables makes the script run instantly, no matter how many times it loops."
[OK] Correct: Even though integers are simple, the script still does one increment per loop, so time grows with the number of loops.
Understanding how simple operations like integer increments scale helps you explain script efficiency clearly and confidently.
"What if we replaced the loop with a nested loop that increments the variable inside both loops? How would the time complexity change?"
Practice
declare -i num=5 do in a bash script?Solution
Step 1: Understand declare -i usage
Thedeclare -icommand in bash makes a variable hold integer values only.Step 2: Analyze the assignment
Here,numis assigned the integer value 5, so it becomes an integer variable.Final Answer:
It creates an integer variable named num with value 5. -> Option AQuick Check:
declare -i num=5 means integer variable num = 5 [OK]
- Thinking declare -i creates a string variable
- Confusing declare -i with printing output
- Assuming declare -i creates a function
count with initial value 10 in bash?Solution
Step 1: Recall integer declaration syntax
In bash,declare -iis used to declare integer variables.Step 2: Check each option
count=10 assigns a value but does not declare integer type. declare count=10 is invalid syntax. declare -i count=10 correctly usesdeclare -i. int count=10 is not valid bash syntax.Final Answer:
declare -i count=10 -> Option CQuick Check:
declare -i count=10 is correct integer declaration [OK]
- Using declare without -i for integers
- Trying C-style int declaration in bash
- Assigning without declare -i loses integer behavior
declare -i x=7 declare -i y=3 x=$x+$y echo $x
Solution
Step 1: Understand variable declarations
Bothxandyare declared as integers with values 7 and 3.Step 2: Analyze the assignment
Parameter expansion producesx=$x+$yx=7+3. Sincexisdeclare -i, bash performs arithmetic evaluation on the RHS: 7 + 3 = 10.Final Answer:
10 -> Option AQuick Check:
$x+$y expands to 7+3, -i evaluates to 10 [OK]
- Thinking it concatenates to 73
- Expecting literal '7+3' output
- Believing explicit $(( )) always required
declare -i total=5 total=total+3 echo $total
Solution
Step 1: Check variable declaration
totalis declared as integer with value 5 [check].Step 2: Analyze assignment
Withtotal=total+3declare -i, RHStotal+3is arithmetic context. Baretotalexpands to 5, evaluates 5+3=8.Final Answer:
8 -> Option BQuick Check:
Bare var in -i RHS expands + computes math [OK]
- Assuming literal 'total+3' without $
- Thinking no math without $(( ))
- Expecting unchanged 5
i. Which snippet correctly increments i inside a loop?Solution
Step 1: Understand integer variable and loop
Declareiinteger=1, loop while <=5, echoi, increment properly to print 1-5.Step 2: Check increment methods
i=i +1parses asi=i(no change) then invalid+1command, fails.i++invalid command without(( )).
No-icauses string concat1+1->11etc.i=$((i+1))explicit arithmetic works portably.Final Answer:
i=$((i+1)) -> Option DQuick Check:
$((i+1)) for safe integer increment [OK]
- Spaces in i=i +1 cause assignment split + command error
- i++ without (( )) is invalid command
- No -i leads to string concatenation
Ready to prove it?
Pass the test and this pattern moves unknown
