Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Calling functions in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Calling functions
Define function
↓
Call function
↓
Execute function body
↓
Return to caller
↓
Continue main script
First, you define a function, then you call it. The script jumps to the function, runs its commands, then returns to continue.
Execution Sample
Bash Scripting
greet() {
  echo "Hello, friend!"
}

greet
Defines a function named greet that prints a message, then calls it to show the message.
Execution Table
StepActionEvaluationOutput
1Define function greetFunction stored
2Call greetJump to greet function
3Execute echo inside greetPrint messageHello, friend!
4Return from greetBack to main script
5End of scriptNo more commands
💡 Script ends after function call completes and no more commands remain
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
greet (function)undefineddefinedcalledexecutedreturneddefined
Key Moments - 3 Insights
Why does the script jump to the function when we call it?
Because calling a function tells the script to pause the main flow and run the function's commands, as shown in step 2 and 3 of the execution_table.
Does the function run automatically when defined?
No, defining a function only stores it. It runs only when called, as seen in step 1 (define) and step 2 (call) in the execution_table.
What happens after the function finishes running?
The script returns to where it left off and continues, shown in step 4 and 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 3?
ANo output
Bgreet function defined
CHello, friend!
DError message
💡 Hint
Check the Output column at step 3 in the execution_table
At which step does the script return from the function?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the Action and Evaluation columns in the execution_table for the return action
If we remove the function call, what changes in the execution_table?
AThe function runs automatically after definition
BThe function never executes and no output appears
CThe script errors out
DThe output appears twice
💡 Hint
Refer to key_moments about when functions run and the execution_table steps
Concept Snapshot
Define a function with name() { commands; }
Call it by writing its name alone.
Calling jumps to function, runs commands, then returns.
Functions do not run when defined, only when called.
Use functions to reuse code and organize scripts.
Full Transcript
In bash scripting, you first define a function by giving it a name and commands inside curly braces. This stores the function but does not run it yet. When you call the function by writing its name, the script pauses the main flow and runs the commands inside the function. After finishing, it returns to continue the main script. This lets you organize your script and reuse code easily. The example shows defining greet, calling it, printing a message, then returning.

Practice

(1/5)
1. In Bash scripting, how do you call a function named greet?
easy
A. Write greet() with parentheses.
B. Write call greet().
C. Just write greet on a line by itself.
D. Write run greet.

Solution

  1. Step 1: Understand Bash function call syntax

    In Bash, functions are called by simply writing their name without parentheses or keywords.
  2. Step 2: Compare options with Bash syntax

    Only Just write greet on a line by itself. matches the correct way: just the function name alone.
  3. Final Answer:

    Just write greet on a line by itself. -> Option C
  4. Quick Check:

    Function call = function name alone [OK]
Hint: Call functions by name only, no parentheses needed [OK]
Common Mistakes:
  • Adding parentheses like in other languages
  • Using keywords like 'call' or 'run'
  • Forgetting to write the function name
2. Which of the following is the correct syntax to define a function named hello in Bash?
easy
A. hello { echo "Hi" }
B. hello() { echo "Hi" }
C. def hello() { echo "Hi" }
D. function hello() echo "Hi"

Solution

  1. Step 1: Recall Bash function definition syntax

    Bash functions are defined as name() { commands; } or with the keyword function followed by name and braces.
  2. Step 2: Check each option

    hello() { echo "Hi" } uses hello() { 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.
  3. Final Answer:

    hello() { echo "Hi" } -> Option B
  4. Quick Check:

    Function syntax = name() { commands } [OK]
Hint: Use name() { commands } to define functions in Bash [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Omitting parentheses or braces
  • Misplacing the function keyword
3. What is the output of this script?
say_hello() {
  echo "Hello, $1!"
}
say_hello Alice
medium
A. Hello, Alice!
B. Hello, $1!
C. say_hello Alice
D. Error: function not found

Solution

  1. Step 1: Understand function and argument usage

    The function say_hello prints "Hello," followed by the first argument $1.
  2. Step 2: Identify the argument passed

    The function is called with argument "Alice", so $1 becomes "Alice".
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    Function argument $1 = Alice [OK]
Hint: Use $1 to access first argument inside function [OK]
Common Mistakes:
  • Printing literal $1 instead of its value
  • Forgetting to pass argument
  • Misunderstanding function call syntax
4. Identify the error in this script:
greet() {
  echo "Hi $name"
}
greet John
medium
A. Function greet is not defined correctly
B. Missing semicolon after echo
C. Function call syntax is wrong
D. Variable $name is not set or passed

Solution

  1. Step 1: Check variable usage inside function

    The function uses $name, but no variable named name is set or passed.
  2. Step 2: Check function call and argument passing

    The function is called with "John" as argument, but the function does not use $1 to access it.
  3. Final Answer:

    Variable $name is not set or passed -> Option D
  4. Quick Check:

    Use $1 to access arguments, not undefined variables [OK]
Hint: Use $1 inside function to get passed arguments [OK]
Common Mistakes:
  • Using undefined variables instead of $1
  • Assuming arguments auto-assign to variable names
  • Ignoring function call syntax correctness
5. You want to write a Bash function sum that takes two numbers as arguments and prints their sum. Which script correctly calls the function and prints the result?
hard
A.
sum() {
  echo $(($1 + $2))
}
sum 3 5
B.
sum() {
  echo $1 + $2
}
sum 3 5
C.
sum() {
  echo $((1 + 2))
}
sum 3 5
D.
sum() {
  echo $1 $2
}
sum 3 5

Solution

  1. Step 1: Understand arithmetic evaluation in Bash

    To add numbers, use $((expression)). Inside, use $1 and $2 for arguments.
  2. Step 2: Check each option's output

    sum() {
      echo $(($1 + $2))
    }
    sum 3 5
    correctly sums $1 + $2.
    sum() {
      echo $1 + $2
    }
    sum 3 5
    prints literal string with plus sign.
    sum() {
      echo $((1 + 2))
    }
    sum 3 5
    adds 1 and 2 literally, ignoring arguments.
    sum() {
      echo $1 $2
    }
    sum 3 5
    prints arguments separated by space.
  3. Final Answer:

    sum() { echo $(($1 + $2)) } sum 3 5 -> Option A
  4. Quick Check:

    Use $(( $1 + $2 )) for sum [OK]
Hint: Use $(( $1 + $2 )) to add arguments inside function [OK]
Common Mistakes:
  • Printing arguments without arithmetic evaluation
  • Using fixed numbers instead of arguments
  • Confusing string concatenation with addition
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