LPIC-1 Shells, Scripting and Data Management Practice Question
A developer needs to ensure a bash script exits immediately if any command fails, and also prints each command before executing it. Which set of shell options should be used at the beginning of the script?
⚠ Common exam trap
Candidates often confuse `-v` (verbose, which prints input lines as read) with `-x` (xtrace, which prints commands before execution), or forget that `-e` is required for exit-on-error, leading them to pick `set -vx` or `set -ux` instead of the correct `set -ex`.
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 -ex
`set -ex` combines two essential shell options: `-e` (errexit) causes the script to exit immediately if any command returns a non-zero exit status, and `-x` (xtrace) prints each command (after expansion) to stderr before executing it. This is the standard way to achieve both behaviors in a single line, as required by the question.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
set -ex
Why this is correct
`set -e` makes the shell exit immediately when any command returns a non-zero status, satisfying the fail-fast requirement. `set -x` enables tracing, printing each command with its expanded arguments to stderr before execution. Combined as `set -ex`, both constraints in the stem are met within a single statement.
- ✗
set -e
Why it's wrong here
set -e alone aborts the script on command failure but prints nothing; the required tracing comes from set -x. It is tempting because -e is the standard errexit idiom, yet the question demands both behaviours, so set -ex (or set -e plus set -x) is needed.
- ✗
set -vx
Why it's wrong here
The -v flag prints shell input lines as read, and -x traces expanded commands, but neither aborts execution on failure; that requires -e. Verbose and xtrace options are for debugging script flow, which is the scenario where set -vx would be chosen.
- ✗
set -ux
Why it's wrong here
The -u flag errors on unset variables and -x traces commands, yet neither terminates the script when a command returns non-zero; only -e does that. Unset-variable checking plus tracing suits scripts where undefined variables are the main hazard, not command failure.
Go deeper
Related to this question
About these practice questions
One of 402 original LPIC-1 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 LPIC-1 practice question is part of Courseiva's free LPI 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 LPIC-1 exam.