Function arguments ($1, $2 inside function) in Bash Scripting - Time & Space Complexity
We want to see how the time to run a bash function changes when it uses arguments like $1 and $2.
How does the number of arguments affect the work inside the function?
Analyze the time complexity of the following code snippet.
my_function() {
echo "First argument: $1"
echo "Second argument: $2"
}
my_function arg1 arg2
This function prints the first two arguments it receives. It uses $1 and $2 inside the function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and printing two arguments once each.
- How many times: Exactly two times, once per argument.
Each argument is accessed and printed once. If we increase the number of arguments, but the function only uses $1 and $2, the work stays the same.
| Input Size (number of arguments) | Approx. Operations |
|---|---|
| 2 | 2 (access and print $1 and $2) |
| 10 | 2 (still only $1 and $2 used) |
| 100 | 2 (no change, only first two arguments matter) |
Pattern observation: The work does not grow with more arguments if the function only uses $1 and $2.
Time Complexity: O(1)
This means the function runs in constant time no matter how many arguments are passed, because it only uses the first two.
[X] Wrong: "More arguments always mean more work inside the function."
[OK] Correct: If the function only uses $1 and $2, extra arguments are ignored, so the work stays the same.
Understanding how function arguments affect work helps you explain efficiency clearly and shows you know how bash functions handle inputs.
"What if the function used a loop to print all arguments using $@? How would the time complexity change?"
Practice
$1 represent?Solution
Step 1: Understand function arguments in bash
Inside a bash function,$1refers to the first argument passed specifically to that function, not the script.Step 2: Differentiate from script arguments
The script's arguments are accessed outside functions, but inside a function,$1resets to the function's first argument.Final Answer:
The first argument passed to the function -> Option AQuick Check:
$1in function = first function argument [OK]
- Confusing $1 inside function with script's $1
- Thinking $1 is function name
- Mixing $1 with number of arguments
$1 and $2?Solution
Step 1: Recall bash function syntax
Bash functions are defined usingname() { commands; }orfunction name { commands; }without parameters in parentheses.Step 2: Identify correct syntax for arguments
Arguments are accessed inside the function via$1,$2, not declared in parentheses.Final Answer:
myfunc() { echo $1 $2; } -> Option DQuick Check:
Function args accessed inside, no params in definition [OK]
- Trying to declare parameters in function definition
- Using other language syntax like def or parentheses with args
- Missing semicolon or braces
greet() {
echo "Hello, $1 and $2!"
}
greet Alice BobSolution
Step 1: Understand function call and arguments
The functiongreetis called with two arguments:AliceandBob. Inside the function,$1isAliceand$2isBob.Step 2: Substitute arguments in echo
The echo command prints: "Hello, Alice and Bob!" exactly as expected.Final Answer:
Hello, Alice and Bob! -> Option BQuick Check:
Function args $1=Alice, $2=Bob replaced correctly [OK]
- Expecting literal $1 and $2 printed
- Mixing argument order
- Forgetting to pass both arguments
sum() {
echo $(($1 + $2))
}
sum 5What error or output will this produce?
Solution
Step 1: Analyze function call with missing argument
The functionsumexpects two arguments, but only one (5) is passed. So,$2is empty.Step 2: Evaluate arithmetic expression
The expression$(($1 + $2))becomes(5 + ), which is invalid syntax causing an error about operand expected.Final Answer:
bash: + : syntax error: operand expected (error token is "+ ") -> Option AQuick Check:
Missing $2 causes arithmetic syntax error [OK]
- Assuming missing args default to zero
- Ignoring error messages
- Passing arguments incorrectly
concat that joins two strings passed as arguments with a dash between them. Which function definition correctly does this?Solution
Step 1: Understand the requirement
The function should join two strings with a dash-between them.Step 2: Check each option's output
concat() { echo "$1-$2"; } prints$1-$2which is exactly the two strings joined by a dash. Others use space, plus, or no separator.Final Answer:
concat() { echo "$1-$2"; } -> Option CQuick Check:
Dash between args = concat() { echo "$1-$2"; } [OK]
- Using space or plus instead of dash
- Forgetting quotes causing word splitting
- Concatenating without separator
Ready to prove it?
Pass the test and this pattern moves unknown
