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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

Save the original value:

$ echo $pre_VENDOR

Assign a new value:

$ VENDOR='Zippy'
$ echo $VENDOR

Restore the original value:

$ echo $VENDOR
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.


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
$ DISPLAY=:1 xterm & # shows the xterm window on display :1
$ echo $DISPLAY

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'
$ (export FOO=other_value; unset BAR; env | grep -E 'FOO|BAR')
$ env | grep -E 'FOO|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


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.