Operating System - HP-UX
1833758 Members
2566 Online
110063 Solutions
New Discussion

Re: Setting ENV variables using a script

 
SOLVED
Go to solution
David Owens_1
Advisor

Setting ENV variables using a script

I have a number of environemnt variables that I need to set while in a shell. Rather than type "export ENV_VAR=value" a number of times, I would like to create a script to set these in my active shell.

I created a script but it does not change the active shell. Only the script environment while running.


Thanks,
David Owens
3 REPLIES 3
Jeff Schussele
Honored Contributor
Solution

Re: Setting ENV variables using a script

Hi David,

Simplest way is to create a file like - let's call it env_set

ENV1=xxxxxx
ENV2=yyyyyy
ENV3=zzzzzz

Then source the file as follows:

. /path/to/env_set

Then the vars remain set for that shell & it's children. BUT they cannot be sent back to that shell's parent.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
James R. Ferguson
Acclaimed Contributor

Re: Setting ENV variables using a script

Hi David:

You simply want to "source" or read a file of the variables you want into your script. You "source" (read) a file with a dot-space character combination before a filename:

. file_to_source

Thus:

# cat /tmp/my.vars
var1="hello"
var2="bye"

# cat /tmp/my.sh
#!/usr/bin/sh
. /tmp/my.vars #...that is DOT-SPACE /tmp...
echo "I said ${var1} and then ${var2}"
exit 0

Now run /tmp/my.sh and you will see that the variables var1 and var2 are in that environment.

Regards!

...JRF...
David Owens_1
Advisor

Re: Setting ENV variables using a script

Thanks all!

The DOT-SPACE is what I needed.

David