Import linux environment variable from shell file

177 Views Asked by At

I want to import environment variables set in my linux user shell file into my perl script ? And i don't want to parse for key=value pairs. How can this be done ?

4

There are 4 best solutions below

0
On

You can use the Envmodule.

$ENV{Variablename}

1
On

Assuming you want to get them from your current shell rather than an arbitrary file, you actually have access to all your environment variables, such as with:

pax> perl -e 'print $ENV{"HOME"}."\n"'
/home/pax

or, if you want to enumerate them all (in sorted order, for example):

foreach $key (sort keys %ENV) {
    print "[$key] -> [$ENV{$key}]\n";
}
0
On

This can now be done with the Env::Modify module.

use Env::Modify 'source';
source("$ENV{HOME}/.my_user_shell_file");
0
On

Look at the solution here: http://darkdiary.ru/users/TLoD_Snake/3233201/comment/

Perl executes your shell command file and reexecutes itself if it figures out that shell command was not executed before.

I've found this solution from TLoD_Snake here on StackOverflow, but I can not find the exact question, so I just point you to his blog.