0
0
Bash Scriptingscripting~20 mins

Environment variables vs local variables in Bash Scripting - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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 $MYVAR
Ahello\n
B\nhello
Chello\nhello
D\n
Attempts:
2 left
💡 Hint
Remember that exporting a variable makes it available to subshells.
💻 Command Output
intermediate
2: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 $MYVAR
Bash Scripting
MYVAR="start"
myfunc() {
  local MYVAR="inside"
  echo $MYVAR
}
myfunc
echo $MYVAR
Astart\ninside
Binside\ninside
Cstart\nstart
Dinside\nstart
Attempts:
2 left
💡 Hint
Local variables inside functions do not affect variables outside.
🔧 Debug
advanced
2:30remaining
Why environment variable is not updated in parent shell?
Consider this script:
export MYVAR="old"
(change_var() {
  MYVAR="new"
  export MYVAR
})
(change_var)
echo $MYVAR

Why does the echo print 'old' instead of 'new'?
Bash Scripting
export MYVAR="old"
(change_var() {
  MYVAR="new"
  export MYVAR
})
(change_var)
echo $MYVAR
ABecause the function runs in a subshell, changes do not affect the parent shell
BBecause export does not update existing environment variables
CBecause MYVAR is local by default inside functions
DBecause the variable was not declared with local keyword
Attempts:
2 left
💡 Hint
Think about where the function runs and how environment variables propagate.
🧠 Conceptual
advanced
1:30remaining
Difference between local and environment variables
Which statement correctly describes the difference between local and environment variables in bash?
ALocal variables exist only within the current shell or function, environment variables are inherited by child processes
BEnvironment variables exist only within the current shell, local variables are inherited by child processes
CLocal variables are automatically exported to child processes, environment variables are not
DEnvironment variables are temporary, local variables are permanent
Attempts:
2 left
💡 Hint
Think about variable visibility and inheritance.
🚀 Application
expert
2: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?
ARun the script normally: ./script.sh
BRun the script with the source command: source ./script.sh
CRun the script in a new terminal window
DAdd local MYVAR in the script before exporting
Attempts:
2 left
💡 Hint
Think about how environment variables propagate between shells.