0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.