# How can I restore the changes to shell variable?

For the objective of an experiment

Suppose I did this

bash="unset DISPLAY; export DISPLAY"


so I can make DISPLAY undefined. Just how can I recover the initial value set? Or at the very least recognize what it was?

3
2022-06-07 14:41:27
Source Share

I'm not exactly sure there is a very easy means to do this (like cd - to return and also forth in between latest cd commands as an example)

What is the context of doing this. do you desire this to be readily available instantly? I am not knowledgeable about this as an attribute of any kind of shell.

One noticeable remedy would certainly be to clearly conserve the variable prior to you transform it, and afterwards later on recover it. Making use of the VENDOR setting variable as an instance:

$echo$VENDOR
intel


Save the initial value:

$pre_VENDOR=$VENDOR
$echo$pre_VENDOR
intel


Assign a new value:

$VENDOR='Zippy'$ echo $VENDOR Zippy  Restore the initial value: $ VENDOR=$pre_VENDOR$ echo $VENDOR intel  3 2022-06-07 15:11:16 Source In basic you write it down prior to hand. If you are inquiring about this theoretically, after that you merely store the old value elsewhere prior to changing it, after that placed it back whenever you desire. If you are inquiring about this due to the fact that currently you are screwed and also are wishing that there is something that will certainly bail you out, after that I have trouble for you 5 2022-06-07 15:11:07 Source You can not, there is no undo. Conserve the value in an additional variable (or in a documents or any place you like) if you intend to have it readily available later on. old_DISPLAY=$DISPLAY
unset DISPLAY
…
DISPLAY=$old_DISPLAY  If you intend to run a solitary command without the setting variable, usage env. env -u DISPLAY emacs  You can additionally make use of the syntax VAR=VALUE COMMAND to set the setting variable VAR to the defined value just for this set command. $ echo $DISPLAY :0$ DISPLAY=:1 xterm & # shows the xterm window on display :1
$echo$DISPLAY
:0


You can not unset a variable in this manner, yet you can set it to a vacant value, which is usually adequate.

You can additionally maintain adjustments neighborhood to a subshell. Parentheses delimit commands to execute 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 different shell with a various setting. Type exit or Ctrl+D to go back to the moms and dad shell. $ bash
$# change the environment, change directories, etc.$ exit
\$ # now you're back to the parent shell with its environment as you left it

1
2022-06-07 15:10:16
Source