XK0-006 Automation, Orchestration, and Scripting Practice Question
In a bash script, a developer needs to parse command-line options such as -f filename and -v (verbose). Which built-in command is best suited for this task?
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
✓
getopts
getopts is a bash built-in for parsing command-line options. It handles short options and requires option arguments.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
getopt
Why it's wrong here
getopt is an external utility that reorders and splits arguments, but the question asks for a shell built-in, and getopt cannot set variables or loop through options natively. It is the correct choice in scripts needing GNU-style long options, whereas the built-in getopts handles -f filename and -v directly.
- ✓
getopts
Why this is correct
getopts is a bash built-in that parses short options with arguments, such as -f filename, and sets OPTARG and OPTIND automatically. It handles the -v flag and option-argument pairing natively, unlike manual shifting or external parsers.
- ✗
case
Why it's wrong here
case performs pattern matching on a value already extracted from the argument list; it does not scan $@, detect leading hyphens, or handle option clustering. It is the correct choice for branching on the option letter once getopts has identified it, not for the parsing step itself.
- ✗
shift
Why it's wrong here
shift discards positional parameters one at a time, so it cannot itself recognise which flag was supplied or consume the filename argument following -f. It is the correct choice inside a getopts loop to advance past processed options, but the parsing logic must come from getopts first.
Go deeper
Related to this question
Key term
Bash script
A Bash script is a text file containing a sequence of commands for the Unix shell Bash, allowing users to automate repetitive tasks and streamline system administration on Linux and macOS.
Key term
Bash
Bash is a command-line interpreter that lets users interact with an operating system by typing text commands instead of clicking icons.
About these practice questions
Courseiva writes every XK0-006 question from scratch — 781 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.