Running a script from script (bash)

Hello

I am a UNIX noob, attempting to use Cygwin on Windows XP to build a program. I am trying to get a few scripts together that can automate some tasks and I have run into a problem.

Basically this is related to calling one script from another. I found this thread in my search but am too noob to know if it answers my problem or not, so I will explain here.

I am calling one script (say: myscript.sh) from my home directory (the script itself is in the cygwin/usr/local/bin directory). Now on one line of this script it calls another script that is on a different drive and folder (say: cygdrive/d/dev/project) and it is called winenv.set.sh.

Now within winenv.set.sh a number of environmental variables are set. But what I notice is that after calling 'myscript.sh' the environmental variables have not been set. I know it works properly if I change into that directory and run the file using: . winenv.set.sh. And that is the same line I am using in 'myscript.sh'.

If I run: bash -v myscript.sh I end up seeing the text of the winenv.set.sh file in the console.

Hopefully my explanation is clear but if not please let me know how I can clarify, and thank you for any help you can provide. :slight_smile:

Here is the solution for those who come across this thread in the future.

The problem in a nutshell is that a script itself runs in a sub-shell, so setting environment variables in that script only sets them in the sub-shell (which is destroyed when the script finishes). The solution is to run the script using the 'source' command which allows the script to change the environment variables in the calling shell.

In my case, I am calling a script that calls a script that sets environment variables:

source myscript.sh

  • and myscript.sh will contain a line: source winenv.set.sh

note that a period can be used instead of 'source'.

. myscript.sh

. winenv.set.sh

More information here: Brian Dessent - Re: setting environment variables from a bash script