Consider the following Bash script, saved as configure.sh, which is executed with bash configure.sh from a shell where neither FOO nor BAR are currently defined:
#!/usr/bin/env bash
FOO=one
hello() {
local FOO=two # scoped only inside the function
BAR=three # ordinary shell variable
export BAR # mark BAR for export
}
hello
unset FOO # remove the global FOO
echo "${FOO:-default}-${BAR}"
Inside hello, local FOO=two hides (but does not overwrite) the global value of FOO, so when the function returns the outer FOO is still one. The assignment BAR=three followed by export BAR marks BAR for export and leaves it set in the current shell because it was not declared local. After the function call, unset FOO removes the global FOO, so it is now unset. The expression ${FOO:-default} therefore substitutes the word default. BAR still contains three, so the final echo prints default-three.
The other choices are incorrect because:
one-three would appear only if FOO had not been unset.
two-three would require the local value of FOO to leak outside the function, which it does not.
default- would require BAR to be unset or not exported, but BAR remains set to three in the script's environment.
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 the purpose of 'local' within a Bash function?
Open an interactive chat with Bash
What does 'unset' do in a Bash script?
Open an interactive chat with Bash
What does 'export' do in Bash and how does it affect variables?
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 .