Unix Shell - dot source filename using $0

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > dot source filename using $0





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author dot source filename using $0
Alvin SIU

2004-07-11, 2:49 am

Hi all,


I have a script named ChildScript

$> cat ChildScript
# -----------------------
# Beginning
# -----------------------
echo $0
# -----------------------
# Ending
# -----------------------

When I running the script like this
$> ChildScript

It will return the name of the script, i.e. ChildScript

When I run the script using dot-source method like this
$> . ChildScript

It CANNOT return the script name ChildScript

How can I get back the script name "ChildScript" ?


Alvin SIU
joe@invalid.address

2004-07-11, 2:49 am

Alvin SIU <alvinsiu001.anti.spam@sinaman.com> writes:

> I have a script named ChildScript
>
> $> cat ChildScript
> # -----------------------
> # Beginning
> # -----------------------
> echo $0
> # -----------------------
> # Ending
> # -----------------------
>
> When I running the script like this
> $> ChildScript
>
> It will return the name of the script, i.e. ChildScript
>
> When I run the script using dot-source method like this
> $> . ChildScript
>
> It CANNOT return the script name ChildScript
>
> How can I get back the script name "ChildScript" ?


$0 gives the name of the currently running shell or script. When you
source another script, it's not running, it's being absorbed by the
currently running script or shell.

If you need to have the name of a script that's being sourced, define
a variable in it that you can examine.

Joe
--
We can't all be heroes because someone has to sit on the curb and
clap as they go by.
- Will Rogers
Chris F.A. Johnson

2004-07-11, 2:49 am

On 2004-07-07, Alvin SIU wrote:
> Hi all,
>
>
> I have a script named ChildScript
>
> $> cat ChildScript
> # -----------------------
> # Beginning
> # -----------------------
> echo $0
> # -----------------------
> # Ending
> # -----------------------
>
> When I running the script like this
> $> ChildScript
>
> It will return the name of the script, i.e. ChildScript
>
> When I run the script using dot-source method like this
> $> . ChildScript
>
> It CANNOT return the script name ChildScript
>
> How can I get back the script name "ChildScript" ?


In ChildScript, put "echo ${script_name:-${0##*/}}".

$ unset script_name
$ script_name=ChildScript
$ . ChildScript
ChildScript
$ ChildScript
ChildScript


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Kenny McCormack

2004-07-18, 5:54 pm

In article <cchpof$8911@imsp212.netvigator.com>,
Alvin SIU <alvinsiu001.anti.spam@sinaman.com> wrote:
>Hi all,
>
>
>I have a script named ChildScript
>
>$> cat ChildScript
># -----------------------
># Beginning
># -----------------------
>echo $0
># -----------------------
># Ending
># -----------------------
>
>When I running the script like this
>$> ChildScript
>
>It will return the name of the script, i.e. ChildScript
>
>When I run the script using dot-source method like this
>$> . ChildScript
>
>It CANNOT return the script name ChildScript


OK

>How can I get back the script name "ChildScript" ?


Apparently, you CANNOT (as you assert earlier).

none

2006-01-29, 9:31 pm

BASH now has a variable called
$BASH_SOURCE
which will contain the source file's name

Kenny McCormack wrote:
> In article <cchpof$8911@imsp212.netvigator.com>,
> Alvin SIU <alvinsiu001.anti.spam@sinaman.com> wrote:
>
>
>
> OK
>
>
>
>
> Apparently, you CANNOT (as you assert earlier).
>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com