Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: how to refer to basename of $0



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