Challenge - 5 Problems
Local Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this Bash script using local variables?
Consider the following Bash script. What will it print when run?
Bash Scripting
my_func() {
local var="inside"
echo "$var"
}
var="outside"
my_func
echo "$var"Attempts:
2 left
💡 Hint
Think about how the local keyword limits the variable scope inside the function.
✗ Incorrect
The local keyword creates a variable that exists only inside the function. So inside the function, var is 'inside'. Outside, var remains 'outside'.
💻 Command Output
intermediate2:00remaining
What error or output does this script produce?
What happens when you run this Bash script?
Bash Scripting
func() {
local x=5
((x++))
echo $x
}
func
echo $xAttempts:
2 left
💡 Hint
Remember local variables do not affect global variables.
✗ Incorrect
Inside func, x is local and incremented to 6 and printed. Outside, x is undefined, so echo prints an empty line.
📝 Syntax
advanced1:30remaining
Which option correctly declares a local variable with initial value in Bash?
Choose the correct syntax to declare a local variable named 'count' with value 10 inside a function.
Attempts:
2 left
💡 Hint
Bash uses = to assign values, no spaces around =.
✗ Incorrect
In Bash, local variables are declared with 'local var=value' syntax. Other options are invalid syntax.
🔧 Debug
advanced2:00remaining
Why does this script print 'local' twice instead of 'local' and 'global'?
Examine the script below and find why the output is not as expected.
Bash Scripting
var="global" func() { var="local" echo "$var" } func echo "$var"
Attempts:
2 left
💡 Hint
Think about variable scope and the effect of missing 'local'.
✗ Incorrect
Without 'local', the assignment inside func changes the global var. So both echoes print 'local'.
🚀 Application
expert2:30remaining
How many variables are accessible outside after running this script?
Given the script below, how many variables named 'a', 'b', and 'c' exist and are accessible outside the function?
Bash Scripting
a=1 func() { local a=2 b=3 local c=4 } func echo "$a $b $c"
Attempts:
2 left
💡 Hint
Local variables exist only inside functions; others affect global scope.
✗ Incorrect
'a' outside is global and unchanged. 'b' is assigned without local, so global. 'c' is local and not accessible outside.