LX0-104 · Question #448
Which of the following commands allows an administrator to make a shell variable visible to subshells?
The correct answer is B. export VARIABLE. The export command is used in a shell to mark a variable so that it becomes visible in the environment of any subshells or child processes that are subsequently launched.
Question
Options
- Aexport $VARIABLE
- Bexport VARIABLE
- Cset $VARIABLE
- Dset VARIABLE
- Eenv VARIABLE
How the community answered
(51 responses)- B88% (45)
- C4% (2)
- D6% (3)
- E2% (1)
Why each option
The `export` command is used in a shell to mark a variable so that it becomes visible in the environment of any subshells or child processes that are subsequently launched.
`export $VARIABLE` attempts to export the *value* of the variable as a new variable name, which is syntactically incorrect and not the way to make a variable visible to subshells.
The `export` command, when followed by a variable name without the dollar sign, marks that variable for export to the environment of subsequent child processes. This makes the variable accessible to any subshells launched from the current shell.
The `set` command is used to display or modify shell options and positional parameters, or to list shell variables, but it does not export variables to subshells.
Similar to choice C, `set VARIABLE` would just display the variable's value if it exists or define a positional parameter, and does not make the variable visible to subshells.
The `env` command displays the current environment or executes a command in a modified environment; `env VARIABLE` would attempt to run `VARIABLE` as a command with an empty environment, which is not the correct usage to export a variable.
Concept tested: Shell variable export and subshell environment
Source: https://www.gnu.org/software/bash/manual/bash.html#index-export
Topics
Community Discussion
No community discussion yet for this question.