LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Source or Execute an external Script from shell script (https://www.linuxquestions.org/questions/linux-newbie-8/source-or-execute-an-external-script-from-shell-script-825584/)

helptonewbie 08-11-2010 11:31 AM

Source or Execute an external Script from shell script
 
Hi All,

I know this is a simple to answer one... but its hard to search google for source'ing or executing a script with '. script'.

Basically, i've realised it seems possible to have one script...
test.sh
Code:

#!/bin/bash
. test1.sh

test1.sh
Code:

#!/bin/bash
echo $@

The thing is...
Code:

./test.sh 123 456

Output:-
123 456

I didn't realise the args of script test.sh would be passed to and able to be used in script test1.sh

Just wondering why this happens more than anything? Its not a problem or anything really... just interested.

Thanks and Regards,
M

GrapefruiTgirl 08-11-2010 12:01 PM

When you source a script, rather than call it explicitly, it inherits the environment of the script which sourced it. Therefore, the parameters passed to the original script initially, are available to the sourced code. When you source a second script into a first one, it acts as though the sourced code is part of the original script, i.e. it acts as though it were all one script.

helptonewbie 08-11-2010 12:48 PM

Ahh course, should have known that really :-)

Thanks and great explaination!


All times are GMT -5. The time now is 10:35 PM.