- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Setting Environment variable in UNIX Shell
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2007 10:27 PM
09-26-2007 10:27 PM
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
Solved! Go to Solution.
- Tags:
- environment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2007 10:31 PM
09-26-2007 10:31 PM
Solutionhttp://developer.apple.com/technotes/tn2002/tn2065.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2007 10:55 PM
09-26-2007 10:55 PM
Re: Setting Environment variable in UNIX Shell
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2007 12:48 AM
09-27-2007 12:48 AM
Re: Setting Environment variable in UNIX Shell
in perl script this is the right environment variable setting:
$ENV{NAME}="ernesto";
Best regards.
Ernesto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2007 09:23 PM
09-27-2007 09:23 PM
Re: Setting Environment variable in UNIX Shell
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2007 10:25 PM
09-27-2007 10:25 PM
Re: Setting Environment variable in UNIX Shell
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2007 10:36 PM
09-27-2007 10:36 PM
Re: Setting Environment variable in UNIX Shell
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.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 12:11 AM
09-28-2007 12:11 AM
Re: Setting Environment variable in UNIX Shell
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 12:32 AM
09-28-2007 12:32 AM
Re: Setting Environment variable in UNIX Shell
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 12:38 AM
09-28-2007 12:38 AM
Re: Setting Environment variable in UNIX Shell
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 12:47 AM
09-28-2007 12:47 AM
Re: Setting Environment variable in UNIX Shell
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...
- Tags:
- source
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 12:57 AM
09-28-2007 12:57 AM
Re: Setting Environment variable in UNIX Shell
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 01:07 AM
09-28-2007 01:07 AM
Re: Setting Environment variable in UNIX Shell
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 01:12 AM
09-28-2007 01:12 AM
Re: Setting Environment variable in UNIX Shell
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 .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 01:17 AM
09-28-2007 01:17 AM
Re: Setting Environment variable in UNIX Shell
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2007 06:13 PM
09-28-2007 06:13 PM
Re: Setting Environment variable in UNIX Shell
Solution i found i have already shown in my reply
thanks and regards
Vikram