Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to set an environment variable (for the parent shell) through a bash script???

185 views
Skip to first unread message

Vinod

unread,
Sep 13, 2004, 1:14:11 PM9/13/04
to
I need to set an env variable in the script.

below are the ways I have tried

***********************test.sh**************************************
#!/bin/bash
export testvar=testvar1
echo $testvar
***********************test.sh**************************************

[wasadmin@u060web4 wasadmin]$./test.sh
testvar1
[wasadmin@u060web4 wasadmin]$echo $testvar

[wasadmin@u060web4 wasadmin]

echo $testvar doesn't return the value. seems like the export doesn't
work for the parent shell only the child shells seems to have the
variable exported.

Next Try


***********************setparams.sh**************************************
#!/bin/bash
export testvar=testvar1
echo $testvar
***********************setparams.sh**************************************


***********************test.sh********************************************
#!/bin/bash
. ./setparams.sh
echo $testvar
***********************test.sh********************************************

[wasadmin@u060web4 wasadmin]$./test.sh
testvar1
[wasadmin@u060web4 wasadmin]$echo $testvar

[wasadmin@u060web4 wasadmin]


Same Result.


Is there a way to set the variable for the Parent Shell. sourcing in
.profile works but the need here is to have it the shell script.


thanks
vinod

Andreas Janssen

unread,
Sep 13, 2004, 1:47:24 PM9/13/04
to
Hello

Vinod (<vinodb...@yahoo.com>) wrote:

> I need to set an env variable in the script.
>
> below are the ways I have tried
>

> [...]


> #!/bin/bash
> export testvar=testvar1
> echo $testvar

> [...]


>
> echo $testvar doesn't return the value. seems like the export doesn't
> work for the parent shell only the child shells seems to have the
> variable exported.

Run

. test.sh

That will execute the script in the current shell.

best regards
Andreas Janssen

--
Andreas Janssen <andreas...@bigfoot.com>
PGP-Key-ID: 0xDC801674 ICQ #17079270
Registered Linux User #267976
http://www.andreas-janssen.de/debian-tipps.html

Rich Gibbs

unread,
Sep 13, 2004, 4:58:30 PM9/13/04
to
Andreas Janssen said the following, on 09/13/04 13:47:

> Hello
>
> Vinod (<vinodb...@yahoo.com>) wrote:
>
>
>> I need to set an env variable in the script.
>>
>> below are the ways I have tried
>>
>> [...]
>> #!/bin/bash
>> export testvar=testvar1
>> echo $testvar
>> [...]
>>
>> echo $testvar doesn't return the value. seems like the export doesn't
>> work for the parent shell only the child shells seems to have the
>> variable exported.
>
>

It is not possible for any process to set the environment of its parent.

>
> Run
>
> .. test.sh


>
> That will execute the script in the current shell.
>

I think you mean to run:

. ./test.sh

At least, that's what works in bash.

> rgibbs@rich02:~/tmp$ cat try.sh


> #!/bin/bash
> export testvar=testvar1
> echo $testvar
>

> rgibbs@rich02:~/tmp$ echo $testvar
>
> rgibbs@rich02:~/tmp$ . ./try.sh
> testvar1
> rgibbs@rich02:~/tmp$ echo $testvar
> testvar1


This works because the initial '.' tells the (current) shell to read and
execute the lines in './try.sh' as input to the current shell process.

(BTW, it is a perfectly natural habit to call things 'test', but it's
really not a good idea -- man test.)

--
Rich Gibbs
rgi...@alumni.princeton.edu
PS: Sorry for multi-posting: I did not notice that AJ had reset followups.

0 new messages