LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   [bash] Exported variable gone after end of script? (https://www.linuxquestions.org/questions/linux-software-2/%5Bbash%5D-exported-variable-gone-after-end-of-script-876481/)

littlebigman 04-22-2011 05:52 AM

[bash] Exported variable gone after end of script?
 
Hello

I'm using a simple Bash script to export an environment variable, eg.

Code:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/my/own/dir
echo $PATH

Then, when I need to add /my/own/dir to PATH, I simply run ./update.path.bash.

Problem is, this change only occurs within the context of the script, and doesn't live beyond it.

What is the right way to update an environment variable so that it "sticks"?

Thank you.

Guttorm 04-22-2011 06:05 AM

Hi

When you start a script, it runs in its own process. A process can't set the environment variables of the parent. But you can run a script without starting a new process. To do this, you have to prefix it with "source" or simply ".".

source ./update.path.bash

or

. ./update.path.bash

littlebigman 04-26-2011 02:33 AM

Thanks for the tip.


All times are GMT -5. The time now is 02:51 AM.