I accidentally sourced the wrong environment from a script. Is there any way to 'unsource' it or in other words to revert it and restore the previous environment?

The obvious answer is to start from a clean shell session, of course, but I'm curious if there's another solution.

Update: I was referring only to a script that sets some variables.

  • 9
    There isn't. Think about it this way: if the script you sourced contained rm -rf /, how would the system undo that? – Satō Katsura Nov 7 '17 at 10:56
  • You'll get a more helpful answer if you showed the wrong sourced file in your question. Please edit your question to improve it. – Basile Starynkevitch Nov 7 '17 at 10:57
  • You don't even need to delete files, just changing the definition of a shell function or changing the value of a shell variable is impossible to undo, unless you happen to know exactly what it was beforehand. But there's no way to know that in general. – ilkkachu Nov 7 '17 at 17:57
  • 1
    @SatōKatsura : The OP is refering to the environment, so I think he might be concerned only with the value of the environment variables. In this case, one could modify the scripts in question by dumping the initial environment variables, and the environment variables at the end of the sourced script, to a file. One would then have to write a small program which computes the necessary commands to undo the changes. Whether this is worth the effort, is a different question. – user1934428 Nov 8 '17 at 7:43
  • 1
    Even if you only care about environment variables it isn't trivial. Dumping variables used in the current script is not enough, since there's a difference between unset foo and foo=. Then the script has to be sourced rather then run, if it has to have any effect on the current environment. Which means temporary variables used by the script need to be restored, since all effects have to be undone. Which leads to an interesting chicken and egg problem. :) – Satō Katsura Nov 8 '17 at 9:11

No, there is no general method for undoing the effects of sourcing a script (or even of "merely" executing one). This is a corollary of the fact that there exist irreversible commands (e.g. file deletion). If your script contains an irreversible command then the effects of sourcing that script will also be irreversible.


Is there any way to 'unsource' it or in other words to revert it and restore previous environment?

No, there is no automatic way in general.

However we don't know what you source-ed. Perhaps it is simple enough to be trivial to undo (e.g. something which just change your PATH from a previously well known one).


If you know ahead of time that you'll want to do this, you can do the source command in a subshell, and revert its effects (insofar as they're scoped to setting shell or environment variables) by exiting that subshell.

Thus, if you run:

(source set-variables; run-command-with-variables); run-command-without-variables

...then run-command-with-variables will have those variables set, but run-command-without-variables will not.

It's also possible to identify which shell functions were set by sourcing a script by checking metadata associated with those functions, such that one could theoretically write a shell function or sourceable script that unsets all other functions defined within a given (parameterized) file. For non-function variables, however, there is not information stored to permit them to be unset (much less identifying prior values).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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