Operating System - HP-UX
1846319 Members
3183 Online
110256 Solutions
New Discussion

Re: Setting environment variables through script

 
SOLVED
Go to solution
devhariprasad
Advisor

Setting environment variables through script

Hi,

I am trying to set an environment variable through a script. When the script is executed, it run in a new shell and the environment variable is available only in this child shell. But how to make it available to even the parent shell.

Please give your ideas.
4 REPLIES 4
Peter Godron
Honored Contributor

Re: Setting environment variables through script

Hi,
please see my example:

$ cat e.sh
#!/usr/bin/sh
echo "DATA before = [$DATA]"
. ./f.sh
echo "DATA after = [$DATA]"

$ cat f.sh
#!/usr/bin/sh
DATA="this is new"
export DATA

$ ./e.sh
DATA before = []
DATA after = [this is new]
Joel Girot
Trusted Contributor
Solution

Re: Setting environment variables through script

Hi,

You can do this, using the dot operator.
Create a small shell script that makes the changes to the environment variable, something like this perhaps:

export MY_PATH=~/bin

Name this shell script myscript.sh and then, from a shell prompt, type:

. myscript.sh

(dot space myscript.sh)
This will run the shell script myscript.sh in your current shell.

Regards

Joel
Dennis Handly
Acclaimed Contributor

Re: Setting environment variables through script

Besides using "." or source (csh), you can use the following trick that resize uses:
$ resize
COLUMNS=80;
LINES=36;
export COLUMNS LINES;

Resize just echoes some commands. Then you just use the eval builting:

$ eval $(resize)
devhariprasad
Advisor

Re: Setting environment variables through script

Thanks a lot for the suggestions