Re: Bash – ”Sourcing” script from another script

Johnny Rosenberg gurus.knugum at gmail.com
Tue Jan 6 18:49:26 UTC 2015


2015-01-06 16:01 GMT+01:00 Karl Auer <kauer at biplane.com.au>:

> On Tue, 2015-01-06 at 14:53 +0100, Johnny Rosenberg wrote:
> > So running a script with source means that the ”#!/bin/sh” (or in my case
> > ”#!/bin/bash”) in MainScript.sh goes for the sourced script too,
> > automatically?
>
> Not sure I understand the question, so I'll answer with a description of
> how things work ;-)
>

Okay, great. Shoot! :P


>
> Terminology: The script doing the sourcing is the main script, the
> script being sourced is the source script.
>
> The source script is syntactically inserted into the main script at the
> point where it is sourced. It is not called, it is not executed, it does
> not get a separate shell process. It becomes part of the main script.
> Whatever is interpreting and executing the main script will interpret
> and execute the source script too.
>
> Any line in the source script that starts with a "#" is treated as a
> comment and ignored. "#!/bin/sh" is only meaningful if it is the very
> first line of a script. It is not special in a source file, because
> there is no way it can ever be the first line.
>
> If you run these commands...
>
>    echo ". /tmp/script2.sh" > /tmp/script1.sh
>    echo '#!/bin/dash' > /tmp/script2.sh
>    echo "ps ax | grep \$\$ | grep -v grep | awk '{print \$5}'"
> >> /tmp/script2.sh
>    chmod u+x /tmp/script1.sh /tmp/script2.sh
>    /tmp/script2.sh ; /tmp/script1.sh
>
> ...you should see this output:
>
>    /bin/dash
>    bash
>
> script2.sh, run on its own, reports dash. But when sourced by
> script1.sh, it reports bash. That is, the first line of script2.sh is
> ignored when script2.sh is sourced, even though it is the first line of
> a source script that is sourced in the first line of a main script.
>
> BTW this also tells you that any executable text file will be treated as
> a bash script by bash if it doesn't have another interpreter specified
> in the first line.
>
> > Does this mean that you can't source, for instance a Python script from a
> > bash script? Or does omitting that line only mean ”nothing is changed,
> keep
> > going”?
>
> You can source anything you like, but if the source file is not correct
> for whatever is interpreting and executing the main file, then the
> source file will cause errors. bash can't interpret python. Yet :-) To
> run python you need a new process, running a python interpreter.
>

That clarified everything, it really did. Thanks!


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ



>
> Regards, K.
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Karl Auer (kauer at biplane.com.au)
> http://www.biplane.com.au/kauer
> http://twitter.com/kauer389
>
> GPG fingerprint: 3C41 82BE A9E7 99A1 B931 5AE7 7638 0147 2C3C 2AC4
> Old fingerprint: EC67 61E2 C2F6 EB55 884B E129 072B 0AF0 72AA 9882
>
>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150106/4a85addc/attachment.html>


More information about the ubuntu-users mailing list