The function increment declares its own local variable named counter. That local variable shadows the global counter for the life of the function call, is incremented to 1, and then is discarded when the function returns. Because the global counter was never modified, the echo statement that follows the function call outputs the original value 0. The script therefore prints 0. The other answers are wrong because:
1 would appear only if the global counter were modified, which it is not.
A blank line would occur only if counter were unset or empty, which is not the case.
An unbound-variable error would occur only if the shell were running with 'set -u' (or 'set -o nounset') and counter were undefined, which again is not true here.
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.
What is a local variable in a Bash script?
Open an interactive chat with Bash
Why doesn't the global 'counter' variable change in this script?
Open an interactive chat with Bash
What is variable shadowing in programming?
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...
IT & Cybersecurity Package Join Premium for Full Access