Bird
0
0

You want to create a reusable library utils.sh with a function timestamp that prints the current date and time. How should you write and use it in your script run.sh to print the timestamp?

hard🚀 Application Q15 of 15
Bash Scripting - Functions
You want to create a reusable library utils.sh with a function timestamp that prints the current date and time. How should you write and use it in your script run.sh to print the timestamp?
AIn utils.sh: <code>timestamp() { date; }</code> and in run.sh: <code>./utils.sh; timestamp</code>
BIn utils.sh: <code>timestamp() { echo $(date); }</code> and in run.sh: <code>source utils.sh; timestamp</code>
CIn utils.sh: <code>function timestamp { echo date; }</code> and in run.sh: <code>./utils.sh; timestamp</code>
DIn utils.sh: <code>timestamp() { date; }</code> and in run.sh: <code>bash utils.sh; timestamp</code>
Step-by-Step Solution
Solution:
  1. Step 1: Define timestamp function correctly

    Use timestamp() { echo $(date); } to print current date and time.
  2. Step 2: Source utils.sh in run.sh and call function

    Use source utils.sh to load the function, then call timestamp.
  3. Final Answer:

    In utils.sh: timestamp() { echo $(date); } and in run.sh: source utils.sh; timestamp -> Option B
  4. Quick Check:

    Correct function and sourcing = In utils.sh: timestamp() { echo $(date); } and in run.sh: source utils.sh; timestamp [OK]
Quick Trick: Use echo $(date) inside function and source before calling [OK]
Common Mistakes:
MISTAKES
  • Running utils.sh instead of sourcing it
  • Using echo date instead of echo $(date)
  • Calling function without sourcing library

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes