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

Hello!

I am trying to set the environment variable, PATH, in my perl script on RedHat 3.

$newbin = "/scratch/bin"; $local_path = $ENV{'PATH'}; $ENV{'PATH'} = $newbin . ":" . $local_path; print "new PATH = $ENV{'PATH'}\n";


This results with:
new PATH = /scratch/bin:/bin
However, after the script is finished, the PATH is only /bin. Is there a more effective way to set environment variables?

Thanks!

Replies are listed 'Best First'.
Re: Setting Environment Variables in Linux
by merlyn (Sage) on Nov 12, 2005 at 02:05 UTC
    The answer to this would be found in perlfaq8 on your hard disk, under the title I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Setting Environment Variables in Linux
by szbalint (Friar) on Nov 12, 2005 at 02:08 UTC

    An effective solution doesn't necessarily mean Perl in this case.

    PATH is most likely set, assuming you're using bash by the file .bash_profile for login shells or by .bashrc for non-login shells in your home directory.

    By the description I was assuming that you were interested in setting the PATH for your shell use.

    From perldoc perlvar: "$ENV{expr} - The hash %ENV contains your current environment. Setting a value in "ENV" changes the environment for any child processes you subsequently fork() off."

    This means a script cannot modify the ENV for it's parent shell, only for it's children processes. A shell is a script's parent process.

Re: Setting Environment Variables in Linux
by sauoq (Abbot) on Nov 12, 2005 at 03:02 UTC

    As you've been told, you can't change your parent process's environment. If you don't mind having an extra shell process sitting around, however, you can exec '/bin/sh'.

    Another possibility is outputting shell commands from your perl script and eval'ing that output in your shell.

    Unless you have a really good reason—like you have real complex things you want to do to your environment—you're probably better off just sourcing a shell script.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Setting Environment Variables in Linux
by Tanktalus (Canon) on Nov 12, 2005 at 17:29 UTC

    As merlyn points out, perlfaq8 answers this. However, it then refers you to another FAQ - comp.unix.questions. That can be found via google easily enough. Just FYI - I use this at work all the time. I have a table that I transform into aliases at work such that I can have these aliases do the "right thing" on AIX, Sun, HP, and Linux boxes. I do that via perl - your path change would fit the same way.

    eval `/common/tools/mkAliases`
    And then the mkAliases perl script does something like this:
    my $table = read_table(); foreach my $key (keys %$table) { print "alias $key '$table->{$key} $current_user';\n"; print "alias ${key}r '$table->{$key} root';\n"; # etc. }
    Note that the most important thing, which I found by trial and error, is that semicolon at the end of each string. The \n's are there in case I want to look at the output, but are ignored by the shell. (Well, not strictly true, but for all practical purposes, it's close enough.)

    What this is doing, then, is creating a shell script in perl that the shell can then run to make the changes to the current shell process.