Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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...

share|improve this answer

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
share|improve this answer

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.

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.