XK0-006 Automation, Orchestration, and Scripting Practice Question
A Bash script uses getopts to parse command-line options. The options are -a (requires an argument) and -b (flag). Which code correctly implements this and stores the argument for -a in $optarg?
⚠ Common exam trap
XK0-006 often tests the case sensitivity of OPTARG and the placement of colons in the option string, so candidates must remember that OPTARG is uppercase and that a colon after an option letter indicates it requires an argument.
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
✓
while getopts 'a:b' opt; do case $opt in a) arg=$OPTARG ;; b) flag=true ;; esac done
The getopts built-in parses options and stores the option argument in the shell variable OPTARG (uppercase). The option string 'a:b' indicates that -a requires an argument (colon after a) and -b is a flag (no colon). The loop uses 'opt' as the variable to hold the current option, and the case statement correctly assigns $OPTARG to arg for -a. This matches option A.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
while getopts 'a:b' opt; do case $opt in a) arg=$OPTARG ;; b) flag=true ;; esac done
Why this is correct
The colon after 'a' in the optstring signals that -a requires an argument, which getopts stores in $OPTARG; 'b' without a colon is a flag. The case statement then assigns $OPTARG to arg, satisfying the requirement.
- ✗
while getopts 'ab:' opt; do case $opt in a) arg=$OPTARG ;; b) flag=true ;; esac done
Why it's wrong here
Placing the colon after b declares -b as the option requiring an argument, leaving -a as a flag, so $OPTARG is never populated for -a. The trailing-colon syntax is correct when the flag option genuinely needs a value, which is the reverse of this scenario's requirement.
- ✗
while getopts 'a:b' opt; do case $opt in a) arg=$optarg ;; b) flag=true ;; esac done
Why it's wrong here
Bash variable names are case-sensitive: getopts populates $OPTARG, not $optarg, so arg is assigned an empty value. The lowercase form would be correct only if the shell variable had actually been set that way, which getopts never does.
- ✗
while getopts ':a:b' opt; do case $opt in a) arg=$OPTARG ;; b) flag=true ;; esac done
Why it's wrong here
The leading colon suppresses getopts' own error messages and sets opt to ':' or '?' for missing/invalid options, but it does not make -a take an argument; the trailing colon after 'a' does that. Without it, -a is treated as a flag and $OPTARG stays empty, so arg is never populated. The colon-prefixed form is correct when you want to handle errors manually.
Go deeper
Related to this question
Learn chapter
Basic Shell Scripting
Key term
Shell
A shell is a computer program that provides a user interface to access an operating system's services, typically by accepting text commands.
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
One of 781 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 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.