[NTLUG:Discuss] OT - include bash script

Robert Citek robert.citek at gmail.com
Wed Jan 6 05:44:50 CST 2010


On Tue, Jan 5, 2010 at 4:22 PM, Greg Edwards <greg at nas-inet.com> wrote:
> Is it possible to have a bash script include code from another file and
> maintain vars set by the parent script?
...
> The $include directive pulls the code fine, but vars set by the parent
> are lost.  In the above example $message is empty.

As others have mentioned, use "source".  And if you are somehow tied
to using $include, you can use your example as-is by assigning source
to $include in the environment:

$ tail -n +1 parent child.inc
==> parent <==
message="hello world"
$include child.inc

==> child.inc <==
echo The message of the day is $message

$ include=source bash parent
The message of the day is hello world

Regards,
- Robert



More information about the Discuss mailing list