LFCS · 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 specific command, the env -u command is used to unset it only for that command's execution.
Question
Options
- Aunset -v FOOBAR
- Bset -a FOOBAR=""
- Cenv -u FOOBAR myscript
- Denv -i FOOBAR myscript
How the community answered
(52 responses)- A4% (2)
- C94% (49)
- D2% (1)
Why each option
To temporarily remove an environment variable for a specific command, the `env -u` command is used to unset it only for that command's execution.
The `unset -v FOOBAR` command unsets the variable in the current shell, making the change permanent for the duration of that shell session, not just for `myscript`.
The `set -a FOOBAR=""` command exports `FOOBAR` as an empty string, meaning it still exists in the environment, rather than being removed.
The `env` command can run a program in a modified environment. The `-u` option specifies a variable to unset in the environment for the command being executed, ensuring it's removed only for `myscript` and not in the current shell.
The `env -i myscript` command executes `myscript` with an *empty* environment, meaning *all* current environment variables would be discarded, not just `FOOBAR`.
Concept tested: Environment variable manipulation with env
Source: https://man7.org/linux/man-pages/man1/env.1.html
Topics
Community Discussion
No community discussion yet for this question.