You want to create a reusable function library utils.sh with a function greet that prints "Hi, <name>!". How should you write and use it in a script?
hard🚀 Application Q8 of 15
Bash Scripting - Functions
You want to create a reusable function library utils.sh with a function greet that prints "Hi, <name>!". How should you write and use it in a script?
AIn <code>utils.sh</code>: <code>def greet(): print("Hi, $1!")</code> and in script: <code>source utils.sh; greet Alice</code>
BIn <code>utils.sh</code>: <code>function greet { echo "Hi, $1!"; }</code> and in script: <code>run utils.sh; greet Alice</code>
CIn <code>utils.sh</code>: <code>greet() { echo "Hi, $1!"; }</code> and in script: <code>source ./utils.sh; greet Alice</code>
DIn <code>utils.sh</code>: <code>greet() { echo "Hi, $1!"; }</code> and in script: <code>import ./utils.sh; greet Alice</code>
Step-by-Step Solution
Solution:
Step 1: Write function in bash syntax
Bash function syntax is greet() { echo "Hi, $1!"; }.
Step 2: Source and call function correctly
To use functions, source the file with source ./utils.sh and call greet Alice. Only In utils.sh: greet() { echo "Hi, $1!"; } and in script: source ./utils.sh; greet Alice uses correct source command.
Final Answer:
In utils.sh: greet() { echo "Hi, $1!"; } and in script: source ./utils.sh; greet Alice -> Option C
Quick Check:
Bash function + source + call = C [OK]
Quick Trick:Define bash functions with () and source before calling [OK]
Common Mistakes:
MISTAKES
Using Python syntax in bash
Using 'run' or 'import' instead of 'source'
Master "Functions" in Bash Scripting
9 interactive learning modes - each teaches the same concept differently