Local variables (local keyword) in Bash Scripting - Time & Space Complexity
When using local variables in bash functions, it is helpful to understand how this affects the script's running time.
We want to see how the use of local changes the cost as the script runs.
Analyze the time complexity of this bash function using local variables.
function count_lines() {
local file="$1"
local count=0
while IFS= read -r line; do
((count++))
done < "$file"
echo "$count"
}
count_lines "myfile.txt"
This function counts the number of lines in a file using local variables inside the function.
Look at what repeats as the script runs.
- Primary operation: Reading each line of the file one by one.
- How many times: Once for every line in the file (n times).
As the file gets bigger, the function reads more lines, so it takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 line reads and increments |
| 100 | About 100 line reads and increments |
| 1000 | About 1000 line reads and increments |
Pattern observation: The time grows roughly in direct proportion to the number of lines.
Time Complexity: O(n)
This means the time to run grows linearly with the number of lines in the file.
[X] Wrong: "Using local variables makes the function run slower because it adds extra work."
[OK] Correct: Declaring variables as local only affects variable scope, not how many times the loop runs. The main cost is reading lines, which stays the same.
Understanding how local variables affect script performance helps you write clear and efficient bash functions, a useful skill in many scripting tasks.
What if we removed the local keyword and used global variables instead? How would the time complexity change?
Practice
local keyword do in a Bash function?Solution
Step 1: Understand variable scope in Bash functions
Variables declared withlocalexist only inside the function where they are declared.Step 2: Compare with other variable types
Global variables exist outside functions;localprevents changes outside the function.Final Answer:
It creates a variable that exists only inside the function. -> Option DQuick Check:
Local variables = function-only scope [OK]
- Thinking local variables are global
- Confusing local with export
- Assuming local deletes variables
count inside a Bash function?Solution
Step 1: Recall correct syntax for local variables
The correct syntax islocal variable_name=value.Step 2: Check each option
Onlylocal count=10matches the correct syntax.Final Answer:
local count=10 -> Option CQuick Check:
local + variable = correct syntax [OK]
- Placing 'local' after variable name
- Using 'var' keyword which doesn't exist
- Mixing 'declare' with 'local' incorrectly
#!/bin/bash
value=5
function test_func() {
local value=10
echo $value
}
test_func
echo $valueSolution
Step 1: Analyze local variable inside function
Insidetest_func,valueis local and set to 10, soecho $valueprints 10.Step 2: Check global variable outside function
Outside the function,valueremains 5 because local variable does not affect it.Final Answer:
10 5 -> Option AQuick Check:
Local inside function, global outside = 10 then 5 [OK]
- Assuming local changes global variable
- Expecting both outputs to be same
- Confusing order of printed values
function example() {
local=5
echo $local
}Solution
Step 1: Check variable naming rules
Usinglocalas a variable name conflicts with the reserved keywordlocal.Step 2: Understand syntax error cause
Assigninglocal=5tries to overwrite the keyword, causing a syntax error.Final Answer:
Using 'local' as a variable name causes a syntax error. -> Option AQuick Check:
Reserved keywords can't be variable names [OK]
- Thinking 'local' can be a variable name
- Ignoring syntax errors from reserved words
- Missing parentheses is not the error here
local to achieve this?Solution
Step 1: Understand local variable purpose
Declaringlocal counterinside the function creates a separate variable that does not affect the global one.Step 2: Check other options
Incrementing global directly changes global variable;exportshares variables with child processes; declaring local outside function is invalid.Final Answer:
Declarelocal counterinside the function and increment it there. -> Option BQuick Check:
Local inside function isolates variable changes [OK]
- Modifying global variable unintentionally
- Misusing export for local scope
- Declaring local variables outside functions
Ready to prove it?
Pass the test and this pattern moves unknown
