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

This is a discussion on Is there a way to set an environment variable (for the parent shell) through a bash script??? within the Linux Administration forums, part of the Linux Forums category; I need to set an env variable in the script. below are the ways I have tried ***********************test.sh******************** ****************** #!/bin/...


Go Back   Usenet Forums > Linux Forums > Linux Administration

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-13-2004
Vinod
 
Posts: n/a
Default Is there a way to set an environment variable (for the parent shell) through a bash script???

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
Reply With Quote
  #2 (permalink)  
Old 09-13-2004
Andreas Janssen
 
Posts: n/a
Default Re: Is there a way to set an environment variable (for the parent shell) through a bash script???

Hello

Vinod (<vinodbijlani@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.janssen@bigfoot.com>
PGP-Key-ID: 0xDC801674 ICQ #17079270
Registered Linux User #267976
http://www.andreas-janssen.de/debian-tipps.html
Reply With Quote
  #3 (permalink)  
Old 09-13-2004
Rich Gibbs
 
Posts: n/a
Default Re: Is there a way to set an environment variable (for the parentshell) through a bash script???

Andreas Janssen said the following, on 09/13/04 13:47:

> Hello
>
> Vinod (<vinodbijlani@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
rgibbs@alumni.princeton.edu
PS: Sorry for multi-posting: I did not notice that AJ had reset followups.



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:50 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0