Challenge - 5 Problems
Environment Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of local vs environment variable in subshell
What is the output of this script?
MYVAR="hello" (export MYVAR; (echo $MYVAR)) echo $MYVAR
Bash Scripting
MYVAR="hello"
(export MYVAR; (echo $MYVAR))
echo $MYVARAttempts:
2 left
💡 Hint
Remember that exporting a variable makes it available to subshells.
✗ Incorrect
The variable MYVAR is set locally and then exported, so the subshell can access it and print 'hello'. The final echo prints the local MYVAR which is still 'hello'.
💻 Command Output
intermediate2:00remaining
Effect of local variable inside function on environment
What is the output of this script?
MYVAR="start"
myfunc() {
local MYVAR="inside"
echo $MYVAR
}
myfunc
echo $MYVARBash Scripting
MYVAR="start" myfunc() { local MYVAR="inside" echo $MYVAR } myfunc echo $MYVAR
Attempts:
2 left
💡 Hint
Local variables inside functions do not affect variables outside.
✗ Incorrect
The function defines a local MYVAR which shadows the global one. Inside the function, it prints 'inside'. Outside, the original MYVAR remains 'start'.
🔧 Debug
advanced2:30remaining
Why environment variable is not updated in parent shell?
Consider this script:
Why does the echo print 'old' instead of 'new'?
export MYVAR="old"
(change_var() {
MYVAR="new"
export MYVAR
})
(change_var)
echo $MYVARWhy does the echo print 'old' instead of 'new'?
Bash Scripting
export MYVAR="old" (change_var() { MYVAR="new" export MYVAR }) (change_var) echo $MYVAR
Attempts:
2 left
💡 Hint
Think about where the function runs and how environment variables propagate.
✗ Incorrect
The function runs inside parentheses, which creates a subshell. Changes to variables in a subshell do not affect the parent shell environment.
🧠 Conceptual
advanced1:30remaining
Difference between local and environment variables
Which statement correctly describes the difference between local and environment variables in bash?
Attempts:
2 left
💡 Hint
Think about variable visibility and inheritance.
✗ Incorrect
Local variables are limited to the current shell or function scope. Environment variables are exported and passed to child processes.
🚀 Application
expert2:00remaining
Preserving environment variable changes after script execution
You have a script that sets and exports an environment variable MYVAR. After running the script normally, the variable is not set in your current shell. How can you modify your usage to have MYVAR set in your current shell after running the script?
Attempts:
2 left
💡 Hint
Think about how environment variables propagate between shells.
✗ Incorrect
Running the script with source executes it in the current shell, so environment variable changes persist after the script finishes.