Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: how to refer to basename of $0
- X-seq: zsh-users 16155
- From: TJ Luoma <luomat@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: how to refer to basename of $0
- Date: Thu, 28 Jul 2011 23:05:04 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=YZkk/5ZsVD0VXURrixFVUCh4NnzsCNtX2sUkzT9lFtU=; b=NO7HeZzYPO63oz+Pqhsw/uZAlrDRVfgE1WcEvo3Xq+12WEpBGDtQFzWpgCHmRUHDDC RQAfAXhlRnZe6C5P+R/gDZXh8DbnVm5rI8t/7D9e3CuzLyme/1EfNoGHt5JtGgDZSy64 jSU72PVDieZNx4sIEbDJ9hnAFtyP9NIFnkMeM=
- In-reply-to: <alpine.LNX.2.01.1107282034110.18508@hp.internal>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CADjGqHuKhx9A3HCUBjU4JgRsbj52s7DQ6HGewT=Y9uRMDGD-fg@mail.gmail.com> <alpine.LNX.2.01.1107282034110.18508@hp.internal>
On Thu, Jul 28, 2011 at 8:44 PM, Benjamin R. Haskell <zsh@xxxxxxxxxx> wrote:
> Sounds like you're not actually 'source'-ing the script. $0 should give you
> the script name in that case. $0 will vary if it's in the top-level scope
> or inside a function, though. For finer-grained control, you can use the
> '(%)' parameter expansion flag on the string "%x". (also see %N, but that
> has the same caveats as $0, AFAIK.)
>
> Try the script below, running in these several ways:
>
> . ./basename-0.zsh
> ./basename-0.zsh
> eval "$(<basename-0.zsh)"
>
> --
> Best,
> Ben
>
> $ cat > basename-0.zsh <<'SCRIPT'
> #!/bin/zsh
>
> echo 0 outside: $0:t
> afunction () { echo 0 in afunction: $0:t }
> afunction
> () { echo 0 in anon: $0:t }
> echo 0 in a subshell: "$(/bin/echo $0:t)"
>
> echo %x outside: ${${(%):-%x}:t}
> anotherfunc () { echo %x in anotherfunc: ${${(%):-%x}:t} }
> anotherfunc
> () { echo %x in anon: ${${(%):-%x}:t} }
> echo %x in a subshell: "$(/bin/echo ${${(%):-%x}:t})"
> SCRIPT
> $ chmod +x basename-0.zsh
I missed the bottom of this email originally… but "$0:t" seems to be
working just fine, and that's exactly the sort of thing I figured Zsh
must have ;-)
Messages sorted by:
Reverse Date,
Date,
Thread,
Author