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

Re: priority of execution

On Tue, 2022-10-25 at 10:17 -0700, Ray Andrews wrote:
> On 2022-10-25 09:08, Peter Stephenson wrote:
> > 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).
> In that order?

Aliases are parsed while stuff is still being read in.  This is why
they're the only type that allow you to use the name of the alias as
some other type of command.

Reserved words are handled after the line has been read in, but still
quite early in parsing, because, as I explained, they need to know about
the structure of what follows.

Everying else is only examined during execution (so if a function
contains the name of one of the following types, it won't be decided
until the function's run which sort it's using --- that's not true of
aliases and reserved words, their effect gets baked into the function).

Shell functions are looked for first.  This means you can replace a
builtin or an external command with a function of the same name, and
still call e.g.

whence() { print This is a special whence; builtin whence "$@"; }
ls() { print Tthis is a special ls; command ls "$@"; }

Builtins are next, which means a builtin with the same name as an
external command is called in preference.  This is usually useful as its
faster, e.g. "test", but sometimes an annoyance, e.g. "enable" is a
shell command but may be an external printer command.  You can disable
builtins you don't need, or use a shell function workaround ("command"
always calls an external command in zsh, unlike other shells, unless
you're in compatibility mode).

External commands are last, with their own rules about the use of $PATH
or absolute paths or those relative to the current directory.

> > Autoloadable functions are a special category of function
> But whence reports them specially:

Yes, just to be helpful.  You'll see once the function has been
autoloaded, it's just a normal function, and if you define a normal
function with that name the autoload flag just vanishes.  It's
really ordering rather than precedence.  If the function's already defined
it's not autoloadable, and it'll ignore attempts to make it so unless
you undefine it first.  Autoloading is just an instruction about how to
get the function if it's not there, it's not really a type of function
at all.


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