Bird
0
0

How can you modify this function to reuse code for printing error messages?

hard🚀 Application Q9 of 15
Bash Scripting - Functions
How can you modify this function to reuse code for printing error messages?
print_error() {
  echo "Error: $1" >&2
}
print_error "File not found"
AUse a function to print errors to standard error stream
BReplace echo with printf without redirecting output
CRemove function and write echo directly each time
DUse echo without redirecting to standard error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the function's purpose

    The function prints error messages to standard error using >&2 redirection.
  2. Step 2: Evaluate options for reusing error printing

    Using a function with redirection centralizes error output, improving reuse.
  3. Final Answer:

    Use a function to print errors to standard error stream -> Option A
  4. Quick Check:

    Functions centralize error printing with >&2 = A [OK]
Quick Trick: Redirect error messages with >&2 inside functions [OK]
Common Mistakes:
MISTAKES
  • Ignoring output redirection
  • Duplicating echo commands
  • Removing function for repeated code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes