Restore previous value or unset environment variable without subshell

  Kiến thức lập trình

This is almost the same (BUT NOT THE SAME!) as this question. I want to save and restore a single environment variable, and leave it unset if it was unset. But unlike that question I don’t have the option of using a subshell because the “middle” command sets some other environment variables I want to keep. I.e.

$ cat ./myenv.sh
export SOME_VAR=1
export OTHER_VAR=1
$ save SOME_VAR
$ source ./myenv.sh
$ restore SOME_VAR

After this SOME_VAR should be unset and OTHER_VAR should be 1. How can I do this?

2

LEAVE A COMMENT