views:

255

answers:

3

I have a shell script, with a list of shell variables, which is executed before entering a programming environment.

I want to use a perl script to enter the programming environment:

system("environment_defaults.sh");
system("obe");

but I when I enter the environment the variables are not set.

+2  A: 

Different sh -c process will be called and env vars are isolated within these.

also doesnt calling environment_defaults.sh also make another sh process within what these variables will be set in isolation?

Or start the perl script with these env. variables exported and these will be set for all its child processes

Imre L
+7  A: 

When you call your second command, it's not done in the environment you modified in the first command. In fact, there is no environment remaining from the first command, because the shell used to invoke "environment_defaults.sh" has already exited.

To keep the context of the first command in the second, invoke them in the same shell:

system("source environment_defaults.sh && obe");

Note that you need to invoke the shell script with source in order to perform its actions in the current shell, rather than invoking a new shell to execute them.

Alternatively, modify your environment at the beginning of every shell (e.g. with .bash_profile, if using bash), or make your environment variable changes in perl itself:

$ENV{FOO} = "hello";
system('echo $FOO');
Ether
this doesnt work as environment_defaults.sh will make it own child sub-instance
Imre L
Thnks, I will try the first option. The other two options are the ones I am trying to avoid.
lamcro
@Imre: true, if one simply executes the script a new shell will be invoked. Use 'source' to perform the commands in the same shell (answer updated).
Ether
Maybe you meant: system("sh -c 'source environment_defaults.sh; obe'")
Grant McLean
+2  A: 

Each process gets its own environment, and each time you call "system" it runs a new process. So, what you are doing won't work. You'll have to run both commands in a single process.

Be aware, however, that after your perl script exists, any environment variables it sets won't be available to you at the command line because your perl script is also a process with its own environment.

Bryan Oakley