zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Ray Andrews <rayandrews@eastlink.ca>, Zsh Users <zsh-users@zsh.org>
Subject: Re: priority of execution
Date: Tue, 25 Oct 2022 17:08:13 +0100 (BST)	[thread overview]
Message-ID: <54517317.3641240.1666714093628@mail.virginmedia.com> (raw)
In-Reply-To: <04b0ce6b-be24-66f0-44e6-0183280e19cf@eastlink.ca>

> On 25/10/2022 16:47 Ray Andrews <rayandrews@eastlink.ca> wrote:
> 
>  
> So far I'm aware of these categories of actionable entities:
> 
> alias, autoload, builtin, function, executable script, binary.

The different categories known to whence, from looking at the source,
are aliases, reserved words, shell functions, builtins, and binaries
in the file system (in the case of whence, those found via $PATH).

Autoloadable functions are a special category of function --- the
difference only comes into play once the shell has decided it's
looking for a shell function, so it's hidden from top-level
execution.

The same goes for shell scripts as a special category of
binaries found in the file system.  The shell sees the script is
a binary in the path and tries to execute it just like anything
else marked as executable.  Typically the OS knows how to deal
specially with scripts, though the shell has this knowledge as
a fallback.

> $ whence -av declare
> declare is a reserved word
> declare is a shell builtin

Reserved words are different from builtins in that they have special
syntax --- the shell knows, for example, that "if" isn't just a command
with arguments, it's got to do a lot more work to handle it.  In
principle, if you turned  it off, the shell *could* have an "if"
builtin as well.  That would obviously be stupid.

You've actually hit a special case here where it's not stupid.
This dates from when declare and its relatives were extended to
handle arrays,

declare array=(bray cray dray)

That needs special parsing because the elements of the array have
to be treated as separate words.

The thing about reserved words is the shell has to be able to see
them in time to parse them.  But the following is perfectly valid
syntax, because "declare" can be used as a normal command:

cmd=declare
$cmd var1 var2 var3

The shell sees $cmd and treats it as the start of a normal command
line for expanding later.  By that time it's already parsed the
arguments as a normal command.  Instead, it just treats "declare"
here as a normal command with arguments.  That's why you see it
show up as a shell builtin as well as a reserved word.

That second case is rather special; you wouldn't encounter it very
often.  But people moan horribly if we break their special cases.

pws


  reply	other threads:[~2022-10-25 16:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-25 15:47 Ray Andrews
2022-10-25 16:08 ` Peter Stephenson [this message]
2022-10-25 17:17   ` Ray Andrews
2022-10-25 18:36     ` Peter Stephenson
2022-10-25 21:58       ` Ray Andrews

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=54517317.3641240.1666714093628@mail.virginmedia.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=rayandrews@eastlink.ca \
    --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).