How to change Linux Terminal environment variable in a perl or bash script?

Hi,
I meet an problem that it cannot change Terminal environment variable in a perl or bash script.
This change can only exist and become effective in script lifetime.
But I want to make this change take effect in current opened Terminal.
In our view, the thought seems to be impossible,
As far as I know, a Source Tool called modules which written by C or Tcl can realize this process.
you can view in

 http://modules.sourceforge.net/

.
Modules -- Software Environment Management.

My problem is that how to change current opened Terminal through a perl or bash script?

Thanks firstly.

cat pathenv
export PATH="$PATH:/opt/X11/bin"

echo $PATH
/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

source pathenv

echo $PATH
/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

Since variables do not go backwards that way, your perl script must create a new shell.

#!/usr/bin/perl

$ENV{MYVAR}="asdf";

exec 'bash', '-sh';
$ echo $MYVAR

$ exec ./myscript.pl # Using exec means you don't need to 'exit' twice after
$ echo $MYVAR
asdf

$

A more flexible version:

#!/usr/bin/perl

$ENV{MYVAR}="asdf";

my $shell=shift;
if($shell) {
        exec $shell, @ARGV;
}
else
{
        exec "bash", "-sh";
}

When not given a shell it assumes bash, otherwise it runs what you tell it on the commandline.

You did a great job.
Thanks very much!
I will try do this method and verify it whether fulfill my requirement.

---------- Post updated 07-25-13 at 08:32 AM ---------- Previous update was 07-24-13 at 09:33 AM ----------

I have already used your solution, but there is some problem as follows:
Our shell version is tcshrc, the terminal prompt like cm280:/home/wch> set in .tcshrc.
But the terminal prompt will become '%' only after executing './1.pl csh' using your method although the variable MYVAR has been changed as 'asdf'.

There cannot change Termina environment MYVAR as 'asdf' at all when executing option './1.pl csh -f'.

Is this any good suggestion and solution in tcshrc ?
I am looking forward to your answer.
Thanks greatly.

-f is the opposite of what you want -- that forces it to not load your tcshrc.

Try csh -l to force it to be a login shell. -l must be the only argument.

Unfortunately, '1.pl csh -l' will also load .cshrc file again. and all variable including MYVAR var will be recovered as setted in .cshrc.

I have tried all csh commandline argument respectively, only -f will not load .cshrc and can modify variable MYVAR.

But after executing '1.pl csh -f', the Terminal Prompt will become '%' only not keeping raw style ctm:/home/wch>

Do you have any good ideas suggestion for me?
Could you try all process as described above in your cshell environment?
Thanks.

That's what you asked for. If you don't want it loaded, you may have to live with the limited prompt.

I have one good idea: Tell me what your perl script is so we can get rid of the perl and turn it into a csh one that can be sourced without creating a new shell.

I have already did a setting of 'setenv LS_BA_STEM LSB_SGE' in .cshrc.

I must modify LS_BA_STEM value for some reason in the perl script in the below and make new value take effect in current Terminal and also the Terminal prompt must keep raw style just as ctm:/home/wch>

I am using the perl script provided by you.

By the way, my requirement is that all these operation should be did in perl script not allowing source a cshell script manually again in terminal after run over 1.pl.

I am very sorry that my perl script is very long and it cannot be converted to cshell at all. The transform is unimaginable.
I think that the perl script transform to cshell script is not a good idea.

You can did a test for following script.

#!/usr/bin/perl

$ENV{MYVAR}="asdf";

my $shell=shift;
if($shell) {
        exec $shell, @ARGV;
}
else
{
        exec "bash", "-sh";
}

Thanks.

I did indeed test it, before I posted it. There is nothing wrong with the perl program. Nothing can stop csh from setting its own variables except csh.

Perhaps you could modify your tcshrc to set the environment only when not already set instead. That way, you can reload your tcshrc and get your fancy prompt without overwriting the important environment variable.

if($ENVVAR == "") then
        setenv ENVVAR "whatever"
endif