Bird
0
0

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:
  1. Step 1: Write function in bash syntax

    Bash function syntax is greet() { echo "Hi, $1!"; }.
  2. 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.
  3. Final Answer:

    In utils.sh: greet() { echo "Hi, $1!"; } and in script: source ./utils.sh; greet Alice -> Option C
  4. 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'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes