The developer expects the script to print 1, but it prints 0. Which single change will make the script print 1while 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.
The keyword local limits the variable's scope to the body of the function, creating a new "shadow" copy of count that disappears when the function returns; the global count therefore remains 0. Removing the word local lets the arithmetic assignment act on the already-defined global variable, so its value becomes 1 and the final echo reflects the increment.
Replacing local with declare -g would also alter the global variable, but declare and its -g option are Bash-specific and therefore break POSIX portability. Capturing the function's output with command substitution (count=$(increment)) runs the function in a subshell, so the parent shell's variable is still unchanged. Simply exporting the variable does not influence whether the function writes to the global or a local copy, so the value would still be 0.
Thus, deleting local is the only modification that meets all stated constraints.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
Why does the 'local' keyword create a new variable in the Bash script?
Open an interactive chat with Bash
What makes 'declare -g -i' not POSIX-compliant?
Open an interactive chat with Bash
Why does using 'count=$(increment)' result in the global variable remaining unchanged?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
Pass with Confidence.
IT & Cybersecurity Package
You have hit the limits of our free tier, become a Premium Member today for unlimited access.
Military, Healthcare worker, Gov. employee or Teacher? See if you qualify for a Community Discount.
Monthly
$19.99
$19.99/mo
Billed monthly, Cancel any time.
3 Month Pass
$44.99
$14.99/mo
One time purchase of $44.99, Does not auto-renew.
MOST POPULAR
Annual Pass
$119.99
$9.99/mo
One time purchase of $119.99, Does not auto-renew.
BEST DEAL
Lifetime Pass
$189.99
One time purchase, Good for life.
What You Get
All IT & Cybersecurity Package plans include the following perks and exams .