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
. /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.


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

  • There isn't any way to get it to stay when I return to the shell? – ahanson Jul 15 '09 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 Jul 15 '09 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. – Elliot Vargas Mar 21 '12 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.