1

I'm trying to write an install script for my Amazon servers and I'm getting stuck on some environment variable issues. I have a set of scripts to configure things and some of them depend on environment variables that I create in profile.d scripts. I create the profile.d script (or copy it over) and need to use the variables it sets in scripts that run later (without logging out and back in).

Is there a way to load these (in a script) so future scripts take advantage of them?

In the script after I create the file I tried:
source /etc/profile.d/scriptname.sh
and
. /etc/profile.d/scriptname.sh
but it only sets the environment variable for the duration of the currently running script, so any other script that gets run later can't use the values being set. How do I get them to get set for the session instead of the script?

I have one master script that calls a series of small scripts to do all the configurations.

1 Answer 1

1

Environmental variables are inherited. So you will need to source the profile scripts at the top of the master script.

3
  • There isn't any way to get it to stay when I return to the shell?
    – ahanson
    Commented Jul 15, 2009 at 20:57
  • 1
    Unfortunately not. A child can never modify the environment of a parent, unless possibly if you do some horrible hacking around in /proc.
    – Cian
    Commented Jul 15, 2009 at 21:10
  • I had a similar problem in which I tried to modify the PATH variable from within a script that was executed from a Bash shell. As @Cian explains, the parent process (in this case the Bash shell) didn't saw the changes because the script is executed as a child process. I was trying to avoid forcing the user to re-login (bash -l). It didn't work. Commented Mar 21, 2012 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .