For the purpose of an experiment...
Suppose I did this
bash="unset DISPLAY; export DISPLAY"
so I can make
DISPLAY undefined. How can I restore the original value set? Or at least know what it was?
In general you write it down before hand.
If you're asking about this in theory, then you simply store the old value somewhere else before modifying it, then put it back whenever you want.
If you're asking about this because now you're screwed and are hoping that there's something that will bail you out, then I have bad news for you...
I'm not sure there's an easy way to do this (like
cd - to move back and forth between most recent
cd commands for instance)
What is the context of doing this .. do you want this to be available automatically? I am not aware of this as a feature of any shell.
One obvious solution would be to explicitly save the variable before you change it, and then later restore it. Using the VENDOR environment variable as an example:
$ echo $VENDOR intel
Save the original value:
$ pre_VENDOR=$VENDOR $ echo $pre_VENDOR intel
Assign a new value:
$ VENDOR='Zippy' $ echo $VENDOR Zippy
Restore the original value:
$ VENDOR=$pre_VENDOR $ echo $VENDOR intel
You can't, there's no undo. Save the value in another variable (or in a file or wherever you like) if you want to have it available later.
old_DISPLAY=$DISPLAY unset DISPLAY … DISPLAY=$old_DISPLAY
If you want to run a single command without the environment variable, use
env -u DISPLAY emacs
You can also use the syntax
VAR=VALUE COMMAND to set the environment variable
VAR to the specified value only for this one command.
$ echo $DISPLAY :0 $ DISPLAY=:1 xterm & # shows the xterm window on display :1 $ echo $DISPLAY :0
You can't unset a variable this way, but you can set it to an empty value, which is often good enough.
You can also keep changes local to a subshell. Parentheses delimit commands to perform in a subshell.
$ env | grep -E 'FOO|BAR' FOO=value_of_FOO BAR=value_of_BAR $ (export FOO=other_value; unset BAR; env | grep -E 'FOO|BAR') FOO=other_value $ env | grep -E 'FOO|BAR' FOO=value_of_FOO BAR=value_of_BAR
You can run a separate shell with a different environment. Type
exit or Ctrl+D to return to the parent shell.
$ bash $ # change the environment, change directories, etc. $ exit $ # now you're back to the parent shell with its environment as you left it