zsh-users
 help / color / mirror / code / Atom feed
From: TJ Luoma <luomat@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: how to refer to basename of $0
Date: Thu, 28 Jul 2011 23:05:04 -0400	[thread overview]
Message-ID: <CADjGqHuJAFwk5wp3qaZL-J_nh9cW6yta2_hdPjF4c9Ogfc7HEg@mail.gmail.com> (raw)
In-Reply-To: <alpine.LNX.2.01.1107282034110.18508@hp.internal>

On Thu, Jul 28, 2011 at 8:44 PM, Benjamin R. Haskell <zsh@benizi.com> 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 ;-)


      parent reply	other threads:[~2011-07-29  3:05 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-28 22:55 TJ Luoma
2011-07-28 23:54 ` Tomasz Moskal
2011-07-29  0:24 ` Phil Pennock
2011-07-29  1:30   ` Tomasz Moskal
2011-07-29  0:44 ` Benjamin R. Haskell
2011-07-29  1:39   ` TJ Luoma
2011-07-29  2:06     ` Tomasz Moskal
2011-07-29  4:04     ` Bart Schaefer
2011-07-29  4:50       ` TJ Luoma
2011-07-29 11:48       ` Tomasz Moskal
2011-07-29 14:08         ` Benjamin R. Haskell
2011-07-29 22:30           ` Bart Schaefer
2011-07-30 19:43             ` Greg Klanderman
2011-07-29  3:05   ` TJ Luoma [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADjGqHuJAFwk5wp3qaZL-J_nh9cW6yta2_hdPjF4c9Ogfc7HEg@mail.gmail.com \
    --to=luomat@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).