What if you could write one function that works for any input without changing its code?
Why Function arguments ($1, $2 inside function) in Bash Scripting? - Purpose & Use Cases
Imagine you have a bash script with a function that needs to work with different file names or user inputs each time you run it. Without function arguments, you have to edit the function code every time to change these values.
Manually changing values inside the function is slow and error-prone. You might forget to update all places or introduce typos. It also makes your script rigid and hard to reuse for different inputs.
Using function arguments like $1, $2 inside a bash function lets you pass different values each time you call it. This makes your function flexible, reusable, and your script easier to maintain.
myfunc() {
echo "Processing file.txt"
}
myfuncmyfunc() {
echo "Processing $1"
}
myfunc file.txtYou can write one function that works with any input, making your scripts powerful and adaptable.
Suppose you want to compress different files using the same function. Passing the file name as an argument lets you compress any file without rewriting the function.
Function arguments let you pass data into functions dynamically.
This avoids hardcoding values and makes scripts reusable.
Using $1, $2 inside functions is simple but powerful.
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
