http://www.perlmonks.org?node_id=590013

heeru has asked for the wisdom of the Perl Monks concerning the following question:

OS = Linux I was trying to set the env variable value for ORACLE_HOME in my program. I was able to do so and printed it succesfully from within the program. However, once i quit the program and return to the shell (csh) and then did an ECHO $ORACLE_HOME, it displays the previous value set from the shell and not the program. Can someone kindly tell me how I can achieve what I want. Thanks!

Replies are listed 'Best First'.
Re: Env Variable value not persistent
by bart (Canon) on Dec 15, 2006 at 11:27 UTC
    In general, child processes (like perl scripts) get a copy of the environment variables from their parent (commonly the shell), to work with. So any change they make to it, won't go back into the parent process.
Re: Env Variable value not persistent
by virtualsue (Vicar) on Dec 15, 2006 at 11:17 UTC
    You can achieve what you want by setting $ORACLE_HOME in your login shell. You do this by putting it in .cshrc or .login in your home directory. If you change the value of an environment variable in a subprocess (e.g. your program), then it will not and indeed should not be reflected in the parent process (the shell, in this case). If you use google or Super Search you will find numerous discussions of this topic.
Re: Env Variable value not persistent
by eyepopslikeamosquito (Archbishop) on Dec 15, 2006 at 11:27 UTC

    Short answer: it can't be done. This question is answered in perlfaq8.

Re: Env Variable value not persistent
by hiseldl (Priest) on Dec 15, 2006 at 15:06 UTC

    I don't know what you will be using it for, but if you just want to run a couple commands, then a sub-shell may work for you; just exit from the subshell, when you're done...

    # I use bash, but you can put csh here, if you want # I added "--norc" so that PS1 is not overridden $ perl -e '$ENV{SQUIRREL}="brown";$ENV{PS1}="subshell> ";system("bash" +, "--norc")' subshell> echo $SQUIRREL brown subshell> exit exit $ echo $SQUIRREL $

    The problem here is that you may forget that you're in a sub-shell and spawn several subshells. One way to address that is to add an environment variable to your script, say, for example, $ENV{MYSCRIPTISRUNNING}="YES", then you could simply exit if this env variable exists, preventing multiple subshells. This is not foolproof, but it's a start.

    N.B. This creates a sub-shell, and the env changes will be lost once you exit all the way out back to your original shell.

    HTH.

    --
    hiseldl
    What time is it? It's Camel Time!