XK0-006 Automation, Orchestration, and Scripting Practice Question
A Bash script uses getopts to parse command-line options. The script is invoked as: ./script -a -b value. Which getopts string should be used to correctly parse -a (no argument) and -b (requires an argument)?
⚠ Common exam trap
XK0-006 often tests the meaning of colons in getopts strings; candidates may reverse the requirement, thinking a colon means no argument, or forget that the colon must follow the option letter.
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
✓
"ab:"
In getopts, a colon after a letter indicates that the option requires an argument. The string 'ab:' means -a takes no argument and -b requires an argument. This matches the invocation './script -a -b value'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
"ab:"
Why this is correct
The trailing colon after b declares that -b consumes an argument, while the bare a marks -a as a flag taking none. This matches the invocation ./script -a -b value, where getopts assigns value to OPTARG for -b and leaves -a argument-free.
- ✗
":ab"
Why it's wrong here
The leading colon only suppresses getopts' own error messages for missing arguments or unknown options; it does not mark b as argument-taking, so -b is treated as a flag. It is tempting because silent error handling suits scripts validating input manually, and would be correct for that purpose.
- ✗
"ab"
Why it's wrong here
Without a colon after b, getopts treats both as boolean flags, leaving value as a positional parameter rather than b's argument. It is tempting because it correctly handles the no-argument flag a, and would be correct if neither option accepted a value.
- ✗
"a:b"
Why it's wrong here
The colon after a places it in argument-requiring mode, so getopts consumes -b as a's value and never parses -b itself. It is tempting because colons do denote required arguments, and would be correct if -a genuinely needed a value, as in "a:b:".
Go deeper
Related to this question
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.
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.
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 and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official CompTIA exam blueprint
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.