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/...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |