Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

EXPORT variables

by sonalig (Acolyte)
on Jun 09, 2008 at 21:54 UTC ( #691108=perlquestion: print w/replies, xml ) Need Help??
sonalig has asked for the wisdom of the Perl Monks concerning the following question:

I need to run the following from the perl script: export PATH=/home/abc/xyz/bin:$PATH How should I do it? I have tried backticks and system, and they don't seem to work. Any help would be appreciated. Thanks in advance

Replies are listed 'Best First'.
Re: EXPORT variables
by friedo (Prior) on Jun 09, 2008 at 22:04 UTC
    export is a bash builtin and not meant to be called from Perl. If you want to altar your process's environment variables, you can do it by modifying the %ENV hash. (See perlvar). For example, $ENV{PATH} = '/home/abc/xyz/bin:' . $ENV{PATH};
      Thanks, Friedo. I am doing the following in the script:
      export PATH; Run a command that requires the above exported PATH.
      When running the script it says, "cannot execute. No file or directory". I am using backticks to execute the command. I tried using system and it didn't work either. Am I missing something? Also,Is there a way to run cp, mv commands of bash from perl scripts?
Re: EXPORT variables
by FunkyMonk (Canon) on Jun 09, 2008 at 22:24 UTC
    Depends exactly what you want to do.

    You're out of luck if you're trying to change PATH for use once your script has exited. If your script changes PATH, it will revert to its former value once your script terminates. This is how shells work in *NIX, and nothing to do with Perl.

    If you want to change PATH for subsequent system calls then just change $ENV{PATH}.

    Unless I state otherwise, all my code runs with strict and warnings
Re: EXPORT variables
by moritz (Cardinal) on Jun 09, 2008 at 22:25 UTC
    Note that you can't modify your caller's environment variables directly. If you write this in perl:
    $ENV{PATH} = "/home/abc/xyz/bin:$ENV{PATH}";

    It will only affect programs you call from that perl script.

    The standard way of altering your caller's environment is to emit shell code, and evaluate that:

    # #!/usr/bin/perl print q{export PATH=/home/abc/xyz/bin:$PATH}, "\n"; # and this is how you call it: $ eval `./`
      Hey, Thanks everyone. U all saved my day. I am now able to run my script. I have one more question: Is there a way in which we can run cp, mv commands etc that run from bash to be executed within a perl script?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://691108]
Approved by friedo
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-08-23 12:31 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (350 votes). Check out past polls.