Calling functions in Bash Scripting - Time & Space Complexity
When we call functions in a bash script, it is important to know how the time to run the script changes as we call these functions more or with bigger inputs.
We want to understand how the number of function calls affects the total work done.
Analyze the time complexity of the following code snippet.
function greet() {
echo "Hello, $1!"
}
for name in Alice Bob Charlie Dave Eve; do
greet "$name"
done
This script defines a function that prints a greeting, then calls it once for each name in a list of five names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the
greetfunction inside a loop. - How many times: Once for each name in the list (5 times here, but depends on list size).
Each time we add one more name, the function is called one more time, so the total work grows directly with the number of names.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 function calls |
| 100 | 100 function calls |
| 1000 | 1000 function calls |
Pattern observation: The total work grows in a straight line as the input size grows.
Time Complexity: O(n)
This means the time to run the script grows directly in proportion to the number of function calls.
[X] Wrong: "Calling a function once or multiple times does not affect the total time much."
[OK] Correct: Each function call takes time, so calling it many times adds up and increases total running time.
Understanding how function calls add up helps you write scripts that run efficiently and shows you can think about how your code behaves as it grows.
"What if the function itself contained a loop that runs n times? How would the time complexity change?"
Practice
greet?Solution
Step 1: Understand Bash function call syntax
In Bash, functions are called by simply writing their name without parentheses or keywords.Step 2: Compare options with Bash syntax
Only Just writegreeton a line by itself. matches the correct way: just the function name alone.Final Answer:
Just writegreeton a line by itself. -> Option CQuick Check:
Function call = function name alone [OK]
- Adding parentheses like in other languages
- Using keywords like 'call' or 'run'
- Forgetting to write the function name
hello in Bash?Solution
Step 1: Recall Bash function definition syntax
Bash functions are defined asname() { commands; }or with the keywordfunctionfollowed by name and braces.Step 2: Check each option
hello() { echo "Hi" } useshello() { echo "Hi" }, which is correct. hello { echo "Hi" } misses the () after the name. def hello() { echo "Hi" } uses Python syntax. function hello() echo "Hi" misses braces.Final Answer:
hello() { echo "Hi" } -> Option BQuick Check:
Function syntax = name() { commands } [OK]
- Using Python or other language syntax
- Omitting parentheses or braces
- Misplacing the function keyword
say_hello() {
echo "Hello, $1!"
}
say_hello AliceSolution
Step 1: Understand function and argument usage
The functionsay_helloprints "Hello," followed by the first argument$1.Step 2: Identify the argument passed
The function is called with argument "Alice", so$1becomes "Alice".Final Answer:
Hello, Alice! -> Option AQuick Check:
Function argument $1 = Alice [OK]
- Printing literal $1 instead of its value
- Forgetting to pass argument
- Misunderstanding function call syntax
greet() {
echo "Hi $name"
}
greet JohnSolution
Step 1: Check variable usage inside function
The function uses$name, but no variable namednameis set or passed.Step 2: Check function call and argument passing
The function is called with "John" as argument, but the function does not use$1to access it.Final Answer:
Variable $name is not set or passed -> Option DQuick Check:
Use $1 to access arguments, not undefined variables [OK]
- Using undefined variables instead of $1
- Assuming arguments auto-assign to variable names
- Ignoring function call syntax correctness
sum that takes two numbers as arguments and prints their sum. Which script correctly calls the function and prints the result?Solution
Step 1: Understand arithmetic evaluation in Bash
To add numbers, use$((expression)). Inside, use$1and$2for arguments.Step 2: Check each option's output
sum() { echo $(($1 + $2)) } sum 3 5correctly sums$1 + $2.sum() { echo $1 + $2 } sum 3 5prints literal string with plus sign.sum() { echo $((1 + 2)) } sum 3 5adds 1 and 2 literally, ignoring arguments.sum() { echo $1 $2 } sum 3 5prints arguments separated by space.Final Answer:
sum() { echo $(($1 + $2)) } sum 3 5 -> Option AQuick Check:
Use $(( $1 + $2 )) for sum [OK]
- Printing arguments without arithmetic evaluation
- Using fixed numbers instead of arguments
- Confusing string concatenation with addition
Ready to prove it?
Pass the test and this pattern moves unknown
