- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- 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
Discussions
Discussions
Forums
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
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