Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a shell variable within a Perl script?

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 when I enter the environment the variables are not set.

like image 968
lamcro Avatar asked Jun 03 '10 16:06

lamcro


People also ask

How do I export a variable in Perl script?

6.3. For a module to export one or more identifiers into a caller's namespace, it must: use the Exporter module, which is part of the standard Perl distribution, declare the module to inherit Exporter's capabilities, by setting the variable @ISA (see Section 7.3. 1) to equal ('Exporter'), and.

How do I export a variable in shell script?

You can use the export command to make local variables global. To make your local shell variables global automatically, export them in your . profile file. Note: Variables can be exported down to child shells but not exported up to parent shells.

How do I export a variable in terminal?

To export a environment variable you run the export command while setting the variable. We can view a complete list of exported environment variables by running the export command without any arguments. To view all exported variables in the current shell you use the -p flag with export.

What does $@ mean in Perl?

In these cases the value of $@ is the compile error, or the argument to die.


1 Answers

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');
like image 190
Ether Avatar answered Oct 03 '22 02:10

Ether