Discussion:
bash: is script being sourced
(too old to reply)
Angel Tsankov
2006-09-13 11:16:31 UTC
Permalink
How can a bash script determine if it is being sourced?
Vilmos Soti
2006-09-13 19:39:38 UTC
Permalink
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
What do you want to do?

Vilmos
Robert Hull
2006-09-13 22:40:46 UTC
Permalink
Post by Vilmos Soti
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
What do you want to do?
Find an answer for her homework?
--
Robert HULL

Archival or publication of this article on any part of thisishull.net
is without consent and is in direct breach of the Data Protection Act
Chris F.A. Johnson
2006-09-13 19:37:30 UTC
Permalink
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
If $0 contains bash, or the name of the shell, it is being sourced;
if it contains the name of the script it is being executed.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Angel Tsankov
2006-09-14 05:30:53 UTC
Permalink
c>> How can a bash script determine if it is being sourced?
Post by Chris F.A. Johnson
If $0 contains bash, or the name of the shell, it is being
sourced;
if it contains the name of the script it is being executed.
What about the case when bash is invoked by su and $0 contains
"-su"?
Michal Nazarewicz
2006-09-14 13:13:29 UTC
Permalink
Post by Chris F.A. Johnson
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
If $0 contains bash, or the name of the shell, it is being sourced;
if it contains the name of the script it is being executed.
Not really:

#v+
$ cat test.sh
echo "\$0: $0"
if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
$ bash test.sh
$0: test.sh
source test.sh
$0: test.sh
#v-

In general there's probably no way to determine this.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Chris F.A. Johnson
2006-09-14 13:43:21 UTC
Permalink
Post by Michal Nazarewicz
Post by Chris F.A. Johnson
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
If $0 contains bash, or the name of the shell, it is being sourced;
if it contains the name of the script it is being executed.
#v+
$ cat test.sh
echo "\$0: $0"
if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
$ bash test.sh
$0: test.sh
source test.sh
$0: test.sh
#v-
In general there's probably no way to determine this.
True, there's no absolute method.

A more accurate statement is: if $0 contains the name of the
calling program, then the script is being sourced. If it contains
the name of the script itself, then it is either being executed, or
it has sourced itself.

If the script never sources itself, then it is possible to tell
(assuming that you have the name of the script hard coded and that
it hasn't been changed).
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Angel Tsankov
2006-09-14 14:59:19 UTC
Permalink
Post by Chris F.A. Johnson
Post by Michal Nazarewicz
Post by Chris F.A. Johnson
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
If $0 contains bash, or the name of the shell, it is being sourced;
if it contains the name of the script it is being
executed.
#v+
$ cat test.sh
echo "\$0: $0"
if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
$ bash test.sh
$0: test.sh
source test.sh
$0: test.sh
#v-
In general there's probably no way to determine this.
True, there's no absolute method.
In fact, I need to determine the full path to the script that is
being executed, either sourced or not.
Chris F.A. Johnson
2006-09-14 15:13:42 UTC
Permalink
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Michal Nazarewicz
Post by Chris F.A. Johnson
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
If $0 contains bash, or the name of the shell, it is being sourced;
if it contains the name of the script it is being
executed.
#v+
$ cat test.sh
echo "\$0: $0"
if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
$ bash test.sh
$0: test.sh
source test.sh
$0: test.sh
#v-
In general there's probably no way to determine this.
True, there's no absolute method.
In fact, I need to determine the full path to the script that is
being executed, either sourced or not.
Why?

If you want the path to a script use 'type -p SCRIPTNAME.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Angel Tsankov
2006-09-14 19:55:46 UTC
Permalink
Post by Chris F.A. Johnson
Post by Angel Tsankov
In fact, I need to determine the full path to the script that
is
being executed, either sourced or not.
Why?
If you want the path to a script use 'type -p SCRIPTNAME.
I have a script that installs ceratin packages and assumes that
they are located in a directory relative to its location. The
location of the script, however, is not known in advance, i.e. it
may be on a cdrom or a fd.
Chris F.A. Johnson
2006-09-15 02:53:41 UTC
Permalink
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
In fact, I need to determine the full path to the script that is
being executed, either sourced or not.
Why?
If you want the path to a script use 'type -p SCRIPTNAME.
I have a script that installs ceratin packages and assumes that
they are located in a directory relative to its location.
Fix the script.
Post by Angel Tsankov
The location of the script, however, is not known in advance, i.e.
it may be on a cdrom or a fd.
If it's not known, how can you execute it?
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Angel Tsankov
2006-09-15 08:20:49 UTC
Permalink
Post by Chris F.A. Johnson
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
In fact, I need to determine the full path to the script
that
is
being executed, either sourced or not.
Why?
If you want the path to a script use 'type -p SCRIPTNAME.
I have a script that installs ceratin packages and assumes
that
they are located in a directory relative to its location.
Fix the script.
Post by Angel Tsankov
The location of the script, however, is not known in advance,
i.e.
it may be on a cdrom or a fd.
If it's not known, how can you execute it?
I meant that the script would not know its own location. However,
users do not where the script is located and they are free to
move it to a different folder.
Chris F.A. Johnson
2006-09-15 12:52:04 UTC
Permalink
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
In fact, I need to determine the full path to the script
that
is
being executed, either sourced or not.
Why?
If you want the path to a script use 'type -p SCRIPTNAME.
I have a script that installs ceratin packages and assumes
that
they are located in a directory relative to its location.
Fix the script.
Post by Angel Tsankov
The location of the script, however, is not known in advance, i.e.
it may be on a cdrom or a fd.
If it's not known, how can you execute it?
I meant that the script would not know its own location.
The script shouldn't need to know its own location.
Post by Angel Tsankov
However, users do not where the script is located and they are free
to move it to a different folder.
If they move it, they know where it is, both origin and
destination.

Configuration files belong in a specific location. System-wide
files belong in /etc; users' own configuration files belong in
$HOME, or some directory under it.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Angel Tsankov
2006-09-15 14:43:27 UTC
Permalink
Post by Chris F.A. Johnson
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
In fact, I need to determine the full path to the script
that
is
being executed, either sourced or not.
Why?
If you want the path to a script use 'type -p SCRIPTNAME.
I have a script that installs ceratin packages and assumes
that
they are located in a directory relative to its location.
Fix the script.
Post by Angel Tsankov
The location of the script, however, is not known in
advance,
i.e.
it may be on a cdrom or a fd.
If it's not known, how can you execute it?
I meant that the script would not know its own location.
The script shouldn't need to know its own location.
OK, but it needs to know where the packages are.
Dan Espen
2006-09-15 15:40:29 UTC
Permalink
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
Post by Chris F.A. Johnson
Post by Angel Tsankov
In fact, I need to determine the full path to the script
that
is
being executed, either sourced or not.
Why?
If you want the path to a script use 'type -p SCRIPTNAME.
I have a script that installs ceratin packages and assumes
that
they are located in a directory relative to its location.
Fix the script.
Post by Angel Tsankov
The location of the script, however, is not known in advance, i.e.
it may be on a cdrom or a fd.
If it's not known, how can you execute it?
I meant that the script would not know its own location.
The script shouldn't need to know its own location.
OK, but it needs to know where the packages are.
Capture that during install.

Dan Espen
2006-09-14 16:47:25 UTC
Permalink
Post by Chris F.A. Johnson
Post by Michal Nazarewicz
Post by Chris F.A. Johnson
Post by Angel Tsankov
How can a bash script determine if it is being sourced?
If $0 contains bash, or the name of the shell, it is being sourced;
if it contains the name of the script it is being executed.
#v+
$ cat test.sh
echo "\$0: $0"
if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
$ bash test.sh
$0: test.sh
source test.sh
$0: test.sh
#v-
In general there's probably no way to determine this.
True, there's no absolute method.
In fact, I need to determine the full path to the script that is being
executed, either sourced or not.
Take a look at the UNIX programming FAQ question 1.14:

http://www.faqs.org/faqs/unix-faq/programmer/faq/
Continue reading on narkive:
Loading...