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

Re: priority of execution

> On 25/10/2022 16:47 Ray Andrews <rayandrews@xxxxxxxxxxx> 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

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


Messages sorted by: Reverse Date, Date, Thread, Author