Bird
0
0

How can you capture both the output message and the return status of a bash function myfunc?

hard🚀 Application Q9 of 15
Bash Scripting - Functions
How can you capture both the output message and the return status of a bash function myfunc?
Aoutput=$(myfunc); status=$(myfunc)
Boutput=$(myfunc); status=$?
Coutput=myfunc; status=myfunc
Dstatus=$(myfunc); output=$?
Step-by-Step Solution
Solution:
  1. Step 1: Capture output with command substitution

    Use output=$(myfunc) to store echoed text.
  2. Step 2: Capture return status with $?

    Immediately after, status=$? stores the function's return code.
  3. Final Answer:

    output=$(myfunc); status=$? -> Option B
  4. Quick Check:

    Output with $(), status with $? = D [OK]
Quick Trick: Use output=$(func); status=$? to capture both [OK]
Common Mistakes:
MISTAKES
  • Mixing output and status variables
  • Calling function twice to get status
  • Assigning status incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes