Include a shell script file in to another schell script like in #include in C

This took a little time to find. But apparently this is very easy. I wanted to share some data in a shell script among some other shell script. So the obvious this to do is to include that shell script inside other shell scripts. To do this we have to use the “source” command.

eg:
!/bin/bash
echo "This is my script"
....
source otherscript.sh
....
echo "Called the other script"

What source command does is just execute the shell script and sharing the execution environment of the currently executing script. If you just call the script as ./otherscript.sh it will just execute with its own stack.

Tags: , , ,

3 Responses to “Include a shell script file in to another schell script like in #include in C”

  1. Jazumaru Says:

    you clarify my doubt
    thank you very much !!

  2. konsolebox Says:

    You may also use Shell Script Loader for implementing a system like #include that only loads scripts once:

    http://loader.sourceforge.net/
    http://www.google.com/search?q=Shell+Script+Loader

  3. BLnewtoLinux Says:

    Thanks for sharing this !!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: