Source environment variable in script

Hi,

I construct a bash script to finish some tasks. One of the task is to set up some environment variables. There is already one script file to complete the environment variables setting work. I am not able to know the detail of how to set these variables. So, I may just need to call this script in my script file.

Assume that my script file is myscript.sh, and another script file is setvar.sh. I start a terminator and run my script. So, the process tree will be like this:

init-login-bash-myscript.sh-setvar.sh

Here is the difficulty: I want these variables to be available in my working shell. But it seems that when I run my script, a child shell will be created, and these variables (set in the setvar.sh) are only available for this child shell.

So, what can I do in my script to let these variables become available for the whole working shell, other than the child shell that is created to run my script.

Source the setvar.sh script in myscript.sh:

. ./setvar.sh
1 Like

Thanks, Franklin.

After doing this, the environment variables are only available for the child shell (created to run myscript.sh), but the environment variables are not available for parent shell. After myscript.sh exits, all these environment variables will die.

Can't you source the setvar script before you start the scripts e.g. in your .profile?

1 Like

YesI cannot access to files such as .profile or .bashrc, etc.