LX0-104 · Question #273
To test a shell script called myscript, the environment variable FOOBAR must be removed temporarily. How can this be done?
The correct answer is C. env -u FOOBAR myscript. To temporarily remove an environment variable for a shell script, the env -u command can execute the script with the specified variable unset.
Question
Options
- Aunset -v FOOBAR
- Bset -a FOOBAR=""
- Cenv -u FOOBAR myscript
- Denv -i FOOBAR myscript
How the community answered
(22 responses)- A5% (1)
- B14% (3)
- C77% (17)
- D5% (1)
Why each option
To temporarily remove an environment variable for a shell script, the `env -u` command can execute the script with the specified variable unset.
`unset -v FOOBAR` would permanently remove the `FOOBAR` variable from the current shell's environment, which is not a temporary removal for only the script.
`set -a FOOBAR=""` would set the `FOOBAR` variable to an empty string and mark it for export, not remove or unset it.
The `env -u FOOBAR myscript` command runs `myscript` with the `FOOBAR` environment variable unset specifically for the duration of that command. This allows testing the script in an isolated environment without affecting the calling shell's environment.
`env -i FOOBAR myscript` would execute `myscript` in an *empty* environment, meaning all inherited variables are removed, which is too broad if only `FOOBAR` needs to be unset while others remain.
Concept tested: Environment variable manipulation using env
Source: https://man7.org/linux/man-pages/man1/env.1.html
Topics
Community Discussion
No community discussion yet for this question.