Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using a dev tool which supplies a script that sets environment variables. If I source the script the environment is modified for the duration of the bash secession. In order to revert the changes made by that script I need to restart the terminal. Is there a way to do it without restarting or opening new bash session?

Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Do it in a subshell:

$ (. /path/to/envsetup.sh; runtool)

If there's just a handful of environment changes, you can do them on the command line:

$ FOO=a BAR=b runtool # FOO and BAR will be set only for runtool

You can also use an actual subshell if you anticipate doing a number of things:

$ bash
$ : this is a subshell
$ . /path/to/envsetup.sh
$ thing1
$ thing2
$ : ...
$ exit
$ : this is the original shell

You may want to include $SHLVL in your $PS1 for the latter, so it's easier to tell that you're in a subshell. I also check for a (nonstandard) environment variable $PSYS and print it if set, as a sort of "label" for the environment.

share|improve this answer
    
H! could you explain this $ (. /path/to/envsetup.sh; runtool). Thanks! P.S. I need the new env vars to stay for a while and then go back to the old env. –  pic11 May 4 '11 at 23:43
    
Enclosing something in parentheses runs it in a temporary subshell. It's kind of like a fast version of my final example, all squeezed into a single line. (The $ is the prompt in all of the above examples.) In your case, it sounds like you want the final version with the full subshell. –  geekosaur May 4 '11 at 23:45
    
How about: type bash then set env and after dode using the dev-tool type exit? –  pic11 May 4 '11 at 23:48
    
That is what the final example does. (As I mentioned before, the $ is the prompt / $PS1, not some special shell syntax.) –  geekosaur May 4 '11 at 23:50
    
I see. Thanks a lot! –  pic11 May 4 '11 at 23:51

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.