XK0-006 Automation, Orchestration, and Scripting Practice Question
A Linux administrator is debugging a Bash script that uses a function to set a global counter. The function increments the variable, but after the function returns, the counter retains its original value. The script does not use subshells or pipelines around the function call. Which of the following is the MOST likely cause?
⚠ Common exam trap
The trap here is attributing the behavior to function definition syntax or export, when the actual cause is the local keyword creating a shadowed, function-scoped variable.
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
✓
The variable was declared with the local keyword inside the function, creating a function-scoped copy that shadows the global variable.
The local keyword inside a function creates a function-scoped variable that shadows any global variable of the same name. Assignments inside the function affect only the local copy, which disappears when the function returns, so the global counter appears unchanged. Removing the local declaration allows the function to modify the global variable directly.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The counter variable was exported with export, which makes it read-only inside functions.
Why it's wrong here
The export builtin marks a variable for inclusion in the environment of child processes; it does not make the variable read-only. Exported variables can still be modified by the current shell and by functions. Read-only behavior requires the readonly builtin or declare -r, not export.
- ✗
The function was defined with the function keyword instead of the POSIX name() syntax, which isolates its variables.
Why it's wrong here
Both the function keyword and the name() syntax define functions in Bash; neither isolates variables by itself. Variable scoping is controlled by the local keyword or by subshell execution, not by the function definition syntax. This is a common misconception but does not explain why the counter reverts.
- ✗
The script lacks a shebang line, so Bash runs it in POSIX mode where functions cannot modify global variables.
Why it's wrong here
A missing shebang may cause the script to be interpreted by a different shell, but POSIX mode does not prevent functions from modifying global variables. Functions in POSIX sh can still assign to global variables. The absence of a shebang is unrelated to the described scoping behavior.
- ✓
The variable was declared with the local keyword inside the function, creating a function-scoped copy that shadows the global variable.
Why this is correct
When a variable is declared with local inside a function, Bash creates a new variable scoped to that function. Any modifications affect only the local copy, and when the function returns, the local variable is destroyed, leaving the global variable unchanged. Removing the local declaration or using a different variable name resolves the issue.
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
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
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.