mediumMultiple ChoiceObjective-mapped
XK0-006 Practice Question: A script uses 'set -e' and then calls a function…
A script uses 'set -e' and then calls a function that returns a non-zero exit status. The script exits unexpectedly. Which of the following should be added to the function to prevent the script from exiting?
⚠ Common exam trap
Candidates often think `return 0` or `exit 0` will override the non-zero exit status, but they fail to realize that `set -e` causes the script to exit immediately when the command fails, before any subsequent `return` or `exit` statement is executed.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
set +e inside the function
The `set -e` directive causes the shell to exit immediately if any command returns a non-zero exit status. When a function called from such a script returns a non-zero status, the script exits. Adding `set +e` inside the function disables this behavior for the function's scope, allowing the function to handle errors internally without terminating the entire script.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
return 0 after the command
Why it's wrong here
This would cause the function to return 0 regardless of the previous command, which is not the same as ignoring an error.
- ✓
set +e inside the function
Why this is correct
Disables exit-on-error for the function, allowing non-zero exit codes without terminating.
- ✗
exit 0
Why it's wrong here
This would exit the entire script with status 0, not just the function.
- ✗
trap '' ERR
Why it's wrong here
This traps the ERR signal but does not prevent the script from exiting due to 'set -e'.
Go deeper
Related to this question
About these practice questions
One of 979 original XK0-006 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.