LPI
010-160 · Question #92
010-160 Question #92: Real Exam Question with Answer & Explanation
The correct answer is E: export MYVAR=value. Shell variables are not automatically inherited by child processes. Using export marks a variable for inclusion in the environment of subsequently executed commands, including subshells like ./script.sh.
Question
The file script.sh in the current directory contains the following content:
#!/bin/bash
echo $MYVAR
The following commands are used to execute this script:
MYVAR=value
./script.sh
The result is an empty line instead of the content of the variable MYVAR. How should MYVAR be set in order to make script.sh display the content of MYVAR?
Options
- A!MYVAR=value
- Benv MYVAR=value
- CMYVAR=value
- D$MYVAR=value
- Eexport MYVAR=value
Explanation
Shell variables are not automatically inherited by child processes. Using export marks a variable for inclusion in the environment of subsequently executed commands, including subshells like ./script.sh.
Common mistakes.
- A.
!MYVAR=valueis not valid shell syntax for variable assignment. - B.
env MYVAR=valuewithout a following command only prints the environment or is incomplete; it does not persistently export the variable for subsequent standalone invocations. - C.
MYVAR=valuesets the variable only in the current shell session and does not export it, so child processes like./script.shcannot access it. - D.
$MYVAR=valueis invalid syntax; variable names on the left side of an assignment must not be prefixed with$.
Concept tested. Exporting shell variables to child process environments
Reference. https://www.gnu.org/software/bash/manual/bash.html#index-export
Community Discussion
No community discussion yet for this question.