Operating System - Linux
1752590 Members
4101 Online
108788 Solutions
New Discussion юеВ

Setting Environment variable in UNIX Shell

 
SOLVED
Go to solution
CA1490051
Frequent Advisor

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
15 REPLIES 15
AwadheshPandey
Honored Contributor
Solution

Re: Setting Environment variable in UNIX Shell

Dennis Handly
Acclaimed Contributor

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
Ernesto Cappello
Trusted Contributor

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
Armin Kunaschik
Esteemed Contributor

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...
Peter Nikitka
Honored Contributor

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"
Ralph Grothe
Honored Contributor

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
CA1490051
Frequent Advisor

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
Peter Nikitka
Honored Contributor

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"
V. Nyga
Honored Contributor

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 ***