CompTIA Linux+ XK0-006 (V8) Practice Question

During a code review you spot the following Bash snippet that is supposed to keep a running counter:

#!/usr/bin/env bash
count=0

increment() {
    local count=$((count + 1))
}

increment
echo "$count"

The developer expects the script to print 1, but it prints 0. Which single change will make the script print 1 while remaining portable to any POSIX-compliant /bin/sh and without starting a subshell?

  • Replace local with declare -g -i count=$((count + 1)) inside the function.

  • Delete the word local so the function assigns directly to the existing count variable.

  • Add export count before the function declaration to make the variable global.

  • Capture the function's output: count=$(increment) instead of calling the function directly.

CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Settings & Objectives
Random Mixed
Questions are selected randomly from all chosen topics, with a preference for those you haven’t seen before. You may see several questions from the same objective or domain in a row.
Rotate by Objective
Questions cycle through each objective or domain in turn, helping you avoid long streaks of questions from the same area. You may see some repeat questions, but the distribution will be more balanced across topics.

Check or uncheck an objective to set which questions you will receive.

Bash, the Crucial Exams Chat Bot
AI Bot