Perl script, how to source an environment variables from .bash_profile

environment, linux, perl, profile, variables

Solution

- The easiest way would be to set the environment variables within perl with `$ENV{"name"}=...`. These variables then will be propagated automatically to any programs started from within the perl script, no matter if perl or shell scripts

- alternatively you could open the .bash_profile, parse it within perl, extract the variables and then set them again within perl with `$ENV`. This is error prone because there might be several ways to declare the variables.

- or you could spawn a shell, which reads the .bash_profile and calls `env` afterwards. Then you read and parse the output from this shell. This is similar to the previous proposal, but the output you have to parse is more clearly defined.

- or you could use a shell script which sources .bash_profile and then spawns the perl script.

Problem

I need some quick advice on perl script. I created a script that basically calls other perl scripts and many other shell scripts within those. The problem I'm facing, is simply trying to make this run on a universal level by setting one environment variable. This is on linux RHEL/CentOS by the way... so I add my variables to .bash_profile and it works without an issue if I MANUALLY source the file first, then run my perl script! This is, OK, but I would like the script to automate this part instead of needing the extra, manual sourcing step. So my script looks like this... in short ``` #!/usr/bin/perl use warnings; use strict; `/bin/bash ~/.bash_profile`; blah blah blah more code etc; ``` When launching the main script (where this part of the code is) it works no problem. It's all the subsequent calls made to other scripts that are failing...as if it is not passing the variable on to the rest of the scripts. Any ideas?? Thanks,

Original source