Setting Environment variable in UNIX Shell (277 Views)
Reply
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 1 of 16 (277 Views)
Accepted Solution

Setting Environment variable in UNIX Shell

Hi All,

I want to set some environment variable through PERL script.

For this i am using the following code
#!/usr/bin/perl
`export HAI=hai`;
$ENV{"HELLO"}="Hello";

Now if i execute the script and do echo $HAI and echo $HELLO in the command prompt, it is not displaying anything.

Please correct me if i am wrong any where.

thanks in advance
Vikram
Please use plain text.
Honored Contributor
AwadheshPandey
Posts: 737
Registered: ‎09-18-2005
Message 2 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,786
Registered: ‎03-06-2006
Message 3 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

I'm not sure wading through Awadhesh's links would find it easily.

But the simple reason is that a child process can't change the environment of the parent.

Several tricks a shell can do is to source a file, really same process. Or return a string and the calling shell just does an eval on it:
$ eval $(echo export foo=bar)
$ env | fgrep foo
foo=bar
Please use plain text.
Trusted Contributor
Ernesto Cappello
Posts: 354
Registered: ‎05-28-2002
Message 4 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi Vikram
in perl script this is the right environment variable setting:

$ENV{NAME}="ernesto";

Best regards.
Ernesto
Please use plain text.
Esteemed Contributor
Armin Kunaschik
Posts: 353
Registered: ‎08-30-1999
Message 5 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

You start a new child process (perl) and then you change the environment of that process.
But environment changes are not available in the parent process.

In shell you would source the script instead of executing it to get the desired behavior.
But this is not possible with a perl script.

My 2 cents,
Armin

PS: Assign points if you find answers useful!
And now for something completely different...
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 6 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi,

like the other posters stated: There is no direct way to move the environment of child to the parent.
If the setup of an environment is the only task of your script, you'll have to generate code, which the parent can execute.
Example for Posix-Shell:

cat myenv.pl
#!/usr/bin/perl
$\ = "\n";
print "export HAI=hello;";
print "export HELP=man;";
...

In the shell, call
eval $(myenv.pl)
print $HAI

This will lead to output 'hello'.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 7 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi Vikram,

can only access the environment of a parent in a child process but not the other way round.

e.g.

$ perl -e '$ENV{GOSSIP}='BlaBla';printf"GOSSIP in in %s: $ENV{GOSSIP}\n",fork?"parent $$":"child $$"'
GOSSIP in parent 19755: BlaBla
GOSSIP in child 19756: BlaBla


Also note, simple key hash definition of the package global hash %ENV is all that is required in Perl to set the environment.
No need for backticks, which would anyway only set the environment for the forked child subprocess via its shell.

Madness, thy name is system administration
Please use plain text.
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 8 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi All,

thank you very much for the replys. I have tried all the options but i am not able to set the environment variable,
I understand it is not possible directly but is there any way from which i can achieve this.

Please suggest some way

thanks and regards
VikraM
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 9 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi,

I don't think, you really tried my example.
What was the output of
myenv.pl

and then, was there any output when calling
eval $(myenv.pl)

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Honored Contributor
V. Nyga
Posts: 4,874
Registered: ‎09-20-2002
Message 10 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi,

either you set the environment you need in the script where you need it or you define it in the start file of your shell.
For an user it's for example .profile for sh-shell and .cshrc for c-shell.

Volkmar
*** Say 'Thanks' with Kudos ***
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 11 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi:

You can't get where you want directly as stated by many. Another variation is to let your Perl script do whatever its objective is and create a file of the variables you want exported. Then, at will, source (read) them into your current environment:

# cat some.pl
#!/usr/bin/perl
open( FH, ">", 'env.sh' ) or die;
print FH "HAI=hai\n";
print FH "HELLO=Hello\n";

# ./some.pl
# . ./env.sh
echo ${HELLO}
Hello

Regards!

...JRF...

Please use plain text.
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 12 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi JRF,

Yes i am also thinking of the same solution but only thing is i have planned to execute both the perl script and the generated shell script through another shell script.

Ex -
!#/usr/bin/sh
Some.pl
chmod +x Env.sh
source Env.sh

Where Env.sh is the generated file.

But if i do this i m getting the error source not found. Can you correct me if i am executing the shell script in wrong way please
thanks and regards
Vikram

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi (again) Vikram:

> Yes i am also thinking of the same solution but only thing is i have planned to execute both the perl script and the generated shell script through another shell script.

Fine, do something like this:

#!/usr/bin/sh
echo "running Perl next"
/home/vikram/perlthing #...outputs /home/vikram/env.sh
. /home/vikram/env.sh #...sources env.sh
echo "Now I can say ${HELLO}"
...

Regards!

...JRF...
Please use plain text.
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 14 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi all,


thank you all for the reply

I got the solution i.e,

execute the perl script through a shell script and write all the env variables into a file and execute this file as source i.e. , by . ./Env.sh and also execute the shell script that is executing the Perl script also as source .
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 15 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Hi (again) Vikram:

By the way:

You don't need to turn the executable bit on to source (read) a file as you showed in your example, above.

Also: If you are using the HP-UX POSIX shell (the standard one) you need to use the dot-space-filename notation to source. If you are using the 'bash' shell, your 'source' notation is fine. NEVER change 'root's shell from '/sbin/sh' to anything else or risk having a system that will not startup after a boot!!!

Regards!

...JRF...
Please use plain text.
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 16 of 16 (277 Views)

Re: Setting Environment variable in UNIX Shell

Thank you all for your valuable suggestions
Solution i found i have already shown in my reply
thanks and regards
Vikram
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation