Bash – ”Sourcing” script from another script

Johnny Rosenberg gurus.knugum at gmail.com
Sun Jan 4 22:09:10 UTC 2015


Just wondering…
I have a script that does two different things related to each other. I
could say that the script does one thing in two distinct steps, so it feels
natural to split those things into two scripts.

However, the second part of the script use some variables that got its
values in the first part, so the second script would need to calculate
those variables again, which feels redundant.

Seems like ”source” would do the job, since it executes the script in the
same shell. I thought something like this:

ScriptA.sh:
SomeVariable=Something
AnotherVariable=SomethingElse
Do-stuff
source ScriptB.sh

ScriptB.sh:
source Some-function-defs.sh
SomeFunction "$SomeVariable"
AnotherFunction "$AnotherVariable"
Do-more-stuff

Some-function-defs.sh:
SomeFunction () { Some action}
AnotherFunction () { More action }

I'm pretty sure it will work, but is this a good practice?
Is it better to keep everything in one file even if that file will be quite
big?
Or is it better to just run the script in another shell sending it a lot of
parameters?


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150104/65aa9b31/attachment.html>


More information about the ubuntu-users mailing list