Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Function arguments ($1, $2 inside function) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Function arguments ($1, $2 inside function)
Define function
↓
Call function with args
↓
Inside function: $1, $2 hold args
↓
Use $1, $2 in commands
↓
Function ends, return to caller
The function is defined, then called with arguments. Inside, $1 and $2 hold the first and second arguments, used in commands.
Execution Sample
Bash Scripting
greet() {
  echo "Hello, $1 and $2!"
}

greet Alice Bob
Defines a function greet that prints a hello message using two arguments passed when calling it.
Execution Table
StepActionValue of $1Value of $2Output
1Define function greet---
2Call greet with Alice BobAliceBob-
3Inside greet: echo messageAliceBobHello, Alice and Bob!
4Function ends, return to caller---
💡 Function completes after printing the message.
Variable Tracker
VariableStartAfter callInside functionAfter function ends
$1--Alice-
$2--Bob-
Key Moments - 2 Insights
Why does $1 hold 'Alice' inside the function?
Because when the function is called (see execution_table step 2), the first argument 'Alice' is assigned to $1 inside the function.
What happens if the function is called with only one argument?
Then $2 will be empty inside the function, so using $2 will produce no output or an empty string (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $2 at step 3?
AHello
BAlice
CBob
DUndefined
💡 Hint
Check the 'Value of $2' column at step 3 in the execution_table.
At which step does the function greet get called with arguments?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step describing the function call in the execution_table.
If we call greet with 'John' only, what will $2 be inside the function?
AEmpty string
BError
CJohn
DUndefined variable
💡 Hint
Refer to key_moments about missing second argument behavior.
Concept Snapshot
Function arguments in bash are accessed inside functions as $1, $2, etc.
$1 is the first argument, $2 the second.
When calling a function, pass arguments separated by spaces.
Inside the function, use $1, $2 to refer to them.
If fewer arguments are passed, missing ones are empty strings.
Full Transcript
This visual trace shows how bash function arguments work. First, a function named greet is defined. Then it is called with two arguments: Alice and Bob. Inside the function, $1 holds 'Alice' and $2 holds 'Bob'. The function uses echo to print a message including these arguments. After printing, the function ends and returns control to the caller. If fewer arguments are passed, the missing ones become empty inside the function. This helps understand how to pass and use arguments in bash functions.

Practice

(1/5)
1. In a bash function, what does $1 represent?
easy
A. The first argument passed to the function
B. The name of the function
C. The total number of arguments passed to the script
D. The exit status of the last command

Solution

  1. Step 1: Understand function arguments in bash

    Inside a bash function, $1 refers to the first argument passed specifically to that function, not the script.
  2. Step 2: Differentiate from script arguments

    The script's arguments are accessed outside functions, but inside a function, $1 resets to the function's first argument.
  3. Final Answer:

    The first argument passed to the function -> Option A
  4. Quick Check:

    $1 in function = first function argument [OK]
Hint: Inside functions, $1 is the first function argument, not script's [OK]
Common Mistakes:
  • Confusing $1 inside function with script's $1
  • Thinking $1 is function name
  • Mixing $1 with number of arguments
2. Which of the following is the correct way to define a bash function that uses arguments $1 and $2?
easy
A. function myfunc { echo $1 $2
B. function myfunc($1, $2) { echo $1 $2 }
C. def myfunc($1, $2) { echo $1 $2 }
D. myfunc() { echo $1 $2; }

Solution

  1. Step 1: Recall bash function syntax

    Bash functions are defined using name() { commands; } or function name { commands; } without parameters in parentheses.
  2. Step 2: Identify correct syntax for arguments

    Arguments are accessed inside the function via $1, $2, not declared in parentheses.
  3. Final Answer:

    myfunc() { echo $1 $2; } -> Option D
  4. Quick Check:

    Function args accessed inside, no params in definition [OK]
Hint: Define functions with () and access args inside, no params in () [OK]
Common Mistakes:
  • Trying to declare parameters in function definition
  • Using other language syntax like def or parentheses with args
  • Missing semicolon or braces
3. What is the output of this script?
greet() {
  echo "Hello, $1 and $2!"
}
greet Alice Bob
medium
A. Hello, $1 and $2!
B. Hello, Alice and Bob!
C. Hello, Alice and !
D. Hello, and Bob!

Solution

  1. Step 1: Understand function call and arguments

    The function greet is called with two arguments: Alice and Bob. Inside the function, $1 is Alice and $2 is Bob.
  2. Step 2: Substitute arguments in echo

    The echo command prints: "Hello, Alice and Bob!" exactly as expected.
  3. Final Answer:

    Hello, Alice and Bob! -> Option B
  4. Quick Check:

    Function args $1=Alice, $2=Bob replaced correctly [OK]
Hint: Arguments replace $1, $2 inside function call [OK]
Common Mistakes:
  • Expecting literal $1 and $2 printed
  • Mixing argument order
  • Forgetting to pass both arguments
4. Consider this function:
sum() {
  echo $(($1 + $2))
}
sum 5

What error or output will this produce?
medium
A. bash: + : syntax error: operand expected (error token is "+ ")
B. 5
C. bash: syntax error: unexpected end of file
D. 10

Solution

  1. Step 1: Analyze function call with missing argument

    The function sum expects two arguments, but only one (5) is passed. So, $2 is empty.
  2. Step 2: Evaluate arithmetic expression

    The expression $(($1 + $2)) becomes (5 + ), which is invalid syntax causing an error about operand expected.
  3. Final Answer:

    bash: + : syntax error: operand expected (error token is "+ ") -> Option A
  4. Quick Check:

    Missing $2 causes arithmetic syntax error [OK]
Hint: Always pass all args used in arithmetic to avoid errors [OK]
Common Mistakes:
  • Assuming missing args default to zero
  • Ignoring error messages
  • Passing arguments incorrectly
5. You want a bash function concat that joins two strings passed as arguments with a dash between them. Which function definition correctly does this?
hard
A. concat() { echo "$1+$2"; }
B. concat() { echo "$1 $2"; }
C. concat() { echo "$1-$2"; }
D. concat() { echo "$1$2"; }

Solution

  1. Step 1: Understand the requirement

    The function should join two strings with a dash - between them.
  2. Step 2: Check each option's output

    concat() { echo "$1-$2"; } prints $1-$2 which is exactly the two strings joined by a dash. Others use space, plus, or no separator.
  3. Final Answer:

    concat() { echo "$1-$2"; } -> Option C
  4. Quick Check:

    Dash between args = concat() { echo "$1-$2"; } [OK]
Hint: Use echo "$1-$2" to join with dash inside function [OK]
Common Mistakes:
  • Using space or plus instead of dash
  • Forgetting quotes causing word splitting
  • Concatenating without separator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard