XK0-006 Automation, Orchestration, and Scripting Practice Question
A Linux administrator is writing a Bash script that accepts a filename as its first argument. The script should print an error and exit if the argument is missing. Which construct should the administrator use to reference the first positional parameter and test whether it is empty?
⚠ Common exam trap
The trap here is mixing up the positional parameter variables, since $0 is the script name and $# is the argument count rather than the first 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
✓
$1 with the -z test, as in [ -z "$1" ]
The first positional parameter is referenced as $1, and quoting it preserves its value even when empty. The -z test evaluates to true for a zero-length string, so checking [ -z "$1" ] reliably detects a missing filename argument and lets the script print an error and exit before proceeding.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
$0 with the -z test, as in [ -z "$0" ]
Why it's wrong here
$0 contains the name of the script itself, not the first argument. It is essentially never empty when the script is invoked, so the test would always be false and the error branch would never run. This does not detect a missing filename argument and is the wrong parameter to check.
- ✓
$1 with the -z test, as in [ -z "$1" ]
Why this is correct
The positional parameter $1 holds the first argument passed to the script. Wrapping it in double quotes preserves empty or whitespace-containing values, and the -z test returns true when the string length is zero, which correctly detects a missing argument and allows the script to print an error and exit.
- ✗
$# with the -z test, as in [ -z "$#" ]
Why it's wrong here
$# holds the count of positional parameters, which is always a numeric string such as 0 or 1. The -z test checks for an empty string, and a count of zero is represented as the character 0, not an empty string, so the test would not behave as intended. The count should be compared numerically instead.
- ✗
$@ with the -z test, as in [ -z "$@" ]
Why it's wrong here
$@ expands to all positional parameters, and when quoted it expands to separate words. Using it with -z in a test is ambiguous because the test receives multiple words when arguments exist, which can cause a syntax error or unexpected result. It is not the correct way to check for a missing first argument.
Go deeper
Related to this question
Learn chapter
Installing Linux and Package Management
Key term
Linux
Linux is an open-source operating system that manages computer hardware and software, widely used in servers, desktops, and embedded systems.
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
This XK0-006 question is part of Courseiva's 781-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.