how to setenv in perl script

Posted on 2003-03-24
Medium Priority
Last Modified: 2010-10-05
Why command below doesn't work? Even I try qx, it also doesn't work....

#!/usr/bin/perl -w

system "setenv P Hello";
print "\nEnvironment Variable P = $ENV{P}\n\n" ;

Pls advice.

Question by:fl78
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 26

Accepted Solution

wilcoxon earned 200 total points
ID: 8197213
Just set the $ENV{P} value directly.

$ENV{P} = 'Hello';
print "\nEnvironment Variable P = $ENV{P}\n\n";
LVL 20

Expert Comment

ID: 8197253
To amplify a bit on what Wilcoxon said...

Environment variables are inherited by sub-processes but do not propagate up to parent processes. The "system" operator and qx operators both create subprocesses to run the command you specify.

The scope of $ENV{P} is the current process and any future children -- it will not have an effect on the shell or other process from which you invoked the Perl script.

Author Comment

ID: 8199268
I tried this:

$ENV{P} = 'Hello';
print "\nEnvironment Variable P = $ENV{P}\n\n";

the screen able to print:

Environment Variable P = Hello

but when I do echo $P on the shell, it prompted to me:

P: Undefined variable.

How to do it in perl so that when echo $P being invoked, it will show me Hello. Pls advice.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 26

Expert Comment

ID: 8199651
I'm not sure what you mean.  These should all work.

$ENV{P} = 'Hello';
system 'echo $P';
system '/bin/sh'; # not sure if this will start an interactive shell or not (haven't tried)

As jmcq said, setting "$ENV{P}" within perl will cause any child processes to inherit $P.  As far as I know there is no way to make a perl %ENV variable persist past the perl process or to make the parent inherit the variable setting.
LVL 20

Expert Comment

ID: 8199657
In order for the action of the perl script to effect a change in an environment variable in the invoking shell, you would have to invoke the perl script in such a way as it's output would be evaluated by the shell and have the perl script output shell commands that, when evaluated by the shell, change the value of the environment variable.

Perl script, let's call it "changeenvP":

print "setenv P Hello\n"

Invoked from shell (assumed to be a C-shell variant because you were using 'setenv' as):

eval `changeenvP`


This is no different than the requirement to use the . or 'source' command to evaluate a shell script that attempts to change environment variables.

Expert Comment

ID: 8200010
I think the problem is that you are not invoking the shell again after making the changes
After running the pgm just do this
if bash
. ~/cshrc

if csh or tcsh
source .~/cshrc

hope that helps

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question