zsh-users
 help / color / mirror / code / Atom feed
* What's the opposite of ZSH_ARGZERO?
@ 2024-03-06 21:56 Felipe Contreras
  2024-03-07  5:16 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Felipe Contreras @ 2024-03-06 21:56 UTC (permalink / raw)
  To: Zsh Users

Hi,

In order to write code that always uses the POSIX $0 regardless of the
POSIX_ARGZERO option one can simply use ZSH_ARGZERO.

But what if someone wants to do the opposite? Use the zsh $0 even if
POSIX_ARGZERO is on?

So far I've found that ${(%):-%N} is the best option, but I wonder if
there is a better one.

Either way it would be nice if there was a variable for that, say ZSH_0.

This also might simplify the zsh plugin standard:

https://wiki.zshell.dev/community/zsh_plugin_standard

Cheers.

-- 
Felipe Contreras


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What's the opposite of ZSH_ARGZERO?
  2024-03-06 21:56 What's the opposite of ZSH_ARGZERO? Felipe Contreras
@ 2024-03-07  5:16 ` Bart Schaefer
  2024-03-15 16:28   ` What's the equivalent of ${BASH_SOURCE[0]}? Felipe Contreras
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2024-03-07  5:16 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: Zsh Users

On Wed, Mar 6, 2024 at 1:57 PM Felipe Contreras
<felipe.contreras@gmail.com> wrote:
>
> But what if someone wants to do the opposite? Use the zsh $0 even if
> POSIX_ARGZERO is on?

Of course the zsh $0 depends on the setting of FUNCTION_ARGZERO, but I
know what you mean.

> So far I've found that ${(%):-%N} is the best option, but I wonder if
> there is a better one.

$funcstack[1] would be the other option, unless for some odd reason
the zsh/parameter module is not available.

An advantage of $funcstack is that you can use
${${(@)funcstack:#'(anon)'}[1]} to get the name of the nearest
non-anonymous function.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What's the equivalent of ${BASH_SOURCE[0]}?
  2024-03-07  5:16 ` Bart Schaefer
@ 2024-03-15 16:28   ` Felipe Contreras
  2024-03-16  5:07     ` Bart Schaefer
  2024-03-16  9:43     ` Roman Perepelitsa
  0 siblings, 2 replies; 8+ messages in thread
From: Felipe Contreras @ 2024-03-15 16:28 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On Wed, Mar 6, 2024 at 11:16 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Wed, Mar 6, 2024 at 1:57 PM Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
> >
> > But what if someone wants to do the opposite? Use the zsh $0 even if
> > POSIX_ARGZERO is on?
>
> Of course the zsh $0 depends on the setting of FUNCTION_ARGZERO, but I
> know what you mean.

I meant the name of the source script file, which I suppose it's
FUNCTION_ARGZERO when you aren't inside a function.

In other words: ${BASH_SOURCE[0]}.

> > So far I've found that ${(%):-%N} is the best option, but I wonder if
> > there is a better one.
>
> $funcstack[1] would be the other option, unless for some odd reason
> the zsh/parameter module is not available.
>
> An advantage of $funcstack is that you can use
> ${${(@)funcstack:#'(anon)'}[1]} to get the name of the nearest
> non-anonymous function.

Actually I realized that it's ${(%):-%x} what I'm looking for, since
that is the best equivalent for ${BASH_SOURCE[0]}.

According to StackOverflow there are no better options [1].

Cheers.

[1] https://stackoverflow.com/a/28336473/10474

-- 
Felipe Contreras


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What's the equivalent of ${BASH_SOURCE[0]}?
  2024-03-15 16:28   ` What's the equivalent of ${BASH_SOURCE[0]}? Felipe Contreras
@ 2024-03-16  5:07     ` Bart Schaefer
  2024-03-16  9:33       ` Felipe Contreras
  2024-03-16  9:43     ` Roman Perepelitsa
  1 sibling, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2024-03-16  5:07 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: Zsh Users

On Fri, Mar 15, 2024 at 9:28 AM Felipe Contreras
<felipe.contreras@gmail.com> wrote:
>
> I meant the name of the source script file

Like $ZSH_SCRIPT ?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What's the equivalent of ${BASH_SOURCE[0]}?
  2024-03-16  5:07     ` Bart Schaefer
@ 2024-03-16  9:33       ` Felipe Contreras
  2024-03-17 21:36         ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Felipe Contreras @ 2024-03-16  9:33 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On Fri, Mar 15, 2024 at 11:07 PM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> On Fri, Mar 15, 2024 at 9:28 AM Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
> >
> > I meant the name of the source script file
>
> Like $ZSH_SCRIPT ?

No. Le'ts get the terminology straight from zsh manpages:

In describing $0 it says:

  If the FUNCTION_ARGZERO option is set, $0 is set ... upon entry to a
  sourced script to the name of the script

So when you do "source ./lib.sh" inside that script "./lib.sh" is the
name of the sourced script.

On the other hand this is the description of $ZSH_SCRIPT:

  If zsh was invoked to run a script, this is the name of the script

So if "lib.sh" is sourced from "foo.sh", then $ZSH_SCRIPT would be
"foo.sh", whereas ${BASH_SOURCE[0]} would be "./lib.sh".

Both are "names of a script", but one is an "invoked script" and the
other is a "sourced script". Correct?

I'm looking for the name of the *sourced* script. So it would be
something like $ZSH_SOURCE (if it existed).

-- 
Felipe Contreras


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What's the equivalent of ${BASH_SOURCE[0]}?
  2024-03-15 16:28   ` What's the equivalent of ${BASH_SOURCE[0]}? Felipe Contreras
  2024-03-16  5:07     ` Bart Schaefer
@ 2024-03-16  9:43     ` Roman Perepelitsa
  2024-03-16 11:59       ` Felipe Contreras
  1 sibling, 1 reply; 8+ messages in thread
From: Roman Perepelitsa @ 2024-03-16  9:43 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: Bart Schaefer, Zsh Users

On Fri, Mar 15, 2024 at 5:29 PM Felipe Contreras
<felipe.contreras@gmail.com> wrote:
>
> Actually I realized that it's ${(%):-%x} what I'm looking for, since
> that is the best equivalent for ${BASH_SOURCE[0]}.
>
> According to StackOverflow there are no better options [1].

Better in what sense? What are the shortfalls of ${(%):-%x} from your
point of view?

Roman.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What's the equivalent of ${BASH_SOURCE[0]}?
  2024-03-16  9:43     ` Roman Perepelitsa
@ 2024-03-16 11:59       ` Felipe Contreras
  0 siblings, 0 replies; 8+ messages in thread
From: Felipe Contreras @ 2024-03-16 11:59 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Bart Schaefer, Zsh Users

On Sat, Mar 16, 2024 at 3:43 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Fri, Mar 15, 2024 at 5:29 PM Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
> >
> > Actually I realized that it's ${(%):-%x} what I'm looking for, since
> > that is the best equivalent for ${BASH_SOURCE[0]}.
> >
> > According to StackOverflow there are no better options [1].
>
> Better in what sense? What are the shortfalls of ${(%):-%x} from your
> point of view?

There aren't any shortfalls, it works fine. It's just complicated.

Given that this seems to be commonly used I just thought perhaps there
was something straightforward like $ZSH_SOURCE.

-- 
Felipe Contreras


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What's the equivalent of ${BASH_SOURCE[0]}?
  2024-03-16  9:33       ` Felipe Contreras
@ 2024-03-17 21:36         ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2024-03-17 21:36 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: Zsh Users

On Sat, Mar 16, 2024 at 2:33 AM Felipe Contreras
<felipe.contreras@gmail.com> wrote:
>
> I'm looking for the name of the *sourced* script. So it would be
> something like $ZSH_SOURCE (if it existed).

I'm back to $funcstack[1] then.

% echo $'print -l $funcstack' > /tmp/saymyname
% Src/zsh -fc 'source /tmp/saymyname'
/tmp/saymyname


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-03-17 21:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-06 21:56 What's the opposite of ZSH_ARGZERO? Felipe Contreras
2024-03-07  5:16 ` Bart Schaefer
2024-03-15 16:28   ` What's the equivalent of ${BASH_SOURCE[0]}? Felipe Contreras
2024-03-16  5:07     ` Bart Schaefer
2024-03-16  9:33       ` Felipe Contreras
2024-03-17 21:36         ` Bart Schaefer
2024-03-16  9:43     ` Roman Perepelitsa
2024-03-16 11:59       ` Felipe Contreras

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).